我必须为大学完成一个项目,但我无法弄清楚它是如何完成的。没有节点的二叉搜索树递归结构
问题是我想用下面给出的函数构建一个二叉查找树应用程序。我需要构建某种递归,但我的问题是bst_insert(tree * bst,int key)函数将树作为输入而不是节点。所以我的想法,我写在下面(bst_insert(bst-> root_node-> left,key);)不起作用。
有人知道我能做些什么来获得可行的解决方案吗?
非常感谢!
这里是我的头文件的一部分(tree.h中)
typedef struct node {
int key;
struct node *left;
struct node *right;
} node;
typedef struct tree {
node *root_node;
int (*compare_keys)(int x, int y);
} tree;
void bst_insert(tree *bst, int key);
这里是tree.c文件
void init(tree *bst) {
bst->root_node = 0;
bst->compare_keys = 0;
}
void bst_insert(tree *bst, int key) {
if (bst->root_node == NULL) {
bst->root_node = (node*)malloc(sizeof(node));
bst->root_node->key = key;
bst->root_node->left = NULL;
bst->root_node->right = NULL;
}
else {
if (key < bst->root_node->key) {
bst_insert(bst->root_node->left, key);
}
if (key > bst->root_node->key) {
bst_insert(bst->root_node->right, key);
}
}
}
你明确地与任何人指定项目不同。你应该可以跟他们谈谈。 – EOF
“函数将树作为输入而不是节点” - 这就是约定。 – babon
你为什么不改变'bst_insert'来取一个节点? – 4386427