在二叉搜索树中,我们将节点结构化为
class Node
{
public:
int key;
Node *left;
Node *right;
};
因此,在创建节点时,我们会这样做
Node* createNode(int key)
{
Node *node=new Node();
node->key=key;
node->left=NULL;
node->right=NULL;
return node;
}
现在我需要关键广告2d矩阵而不是整数关键字。像这样的东西
class Node
{
public:
char matrix[3][3];
Node *left;
Node *right;
};
现在如何创建节点?
Node* createNode(char key[3][3])
{
Node *node=new Node();
node->matrix=key; //This line
return node;
}
答案 0 :(得分:2)
在C ++中它同样简单:
vector<vector<double> > matrix;
vector<double> &row = matrix[i];
在C中
double *row = Matrix[i];
请注意,C语言也适用于C ++,但是你应该尽可能地选择容器到普通指针。
答案 1 :(得分:0)
要获得一排,这很简单,就像@dasblinkenlight所说的那样。然而,要获得一个专栏,它涉及更多。假设您知道矩阵中的行数,您可以执行以下操作:
int *copyColumn(int **matrix, int column, int rows)
{
int *data = new int[rows];
for (int i = 0; i < rows; i++)
data[i] = matrix[i][column];
return data;
}
完成后,请记住delete
数据!