谢谢,现在由于某种原因它没有按预期工作。当我运行该程序时,它只是给出一个错误“bst.exe 已停止工作”,它发生在这个函数中。
static NODE *insert_i(NODE *r, int x)
{
NODE *leaf;
while(r)
{
if(r->val == x)
return r;
if(x < r->val && r->left != NULL)
r = r->left;
else if(x > r->val && r->right != NULL)
r = r->right;
}
leaf = malloc(sizeof(NODE));
leaf->left = NULL;
leaf->right = NULL;
leaf->val = x;
count++;
if(x < r->val)
r->left = leaf;
else
r->right = leaf;
return r;
}
void bst_insert(BST_PTR t, int x)
{
t->root = insert_i(t->root, x);
}
最佳答案
你有
while(r)
{
if(r == NULL)
if
条件永远不会为真,就好像r
是NULL
那么循环就会结束,不从函数返回任何内容。
关于c - 二叉搜索树插入(C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20469795/