谢谢,现在由于某种原因,它没有按预期工作。当我运行程序时它只是给出一个错误“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);
}
答案 0 :(得分:4)
你有
while(r)
{
if(r == NULL)
if
条件永远不会为真,如果r
为NULL
,那么循环将结束,而不会从函数返回任何内容。
答案 1 :(得分:1)
如果在没有返回的情况下未输入或退出while
循环,会发生什么?它不会返回任何内容,行为将不确定。
因此,返回NULL表示未找到或将if(r==NULL)
移出循环。它不会在循环内执行。