【发布时间】:2016-09-24 12:24:05
【问题描述】:
最近我试图操纵二叉搜索树并被困在这里。我想要一个数组(指针数组),我想在其中按顺序存储二叉搜索树的每个节点的指针。我不需要每个节点的值我需要指针,以便我可以访问它们的值,左子树和右子树。我所做的是
struct node{
int key;
struct node *left, *right;
};
node **arr;
int x=0;
void inorder(struct node *root){
if (root != NULL){
inorder(root->left);
//cout<<"X : "<<x<<endl;
arr[x] = root;
x++;
printf("%d \n", root->key);
inorder(root->right);
}
}
请帮忙。谢谢。
【问题讨论】:
-
看起来不错。怎么了?
-
问题是当我使用 arr[i]->value 时会报错。
-
什么样的错误?它可能应该是 arr[i]->Key 虽然...
-
我正在使用 Dev C++,它显示了一个错误,例如“[Error] base operand of '->' has non-pointer type 'node'”
-
错误信息似乎提示 arr[i].key,但这似乎与您发布的代码不一致。
标签: pointers data-structures tree binary-tree binary-search-tree