谢谢,现在由于某种原因,它不能按预期工作。当我运行程序时,它只是给出了一个错误“bst.exe已停止工作”,它发生在这个函数中。二进制搜索树插入(C)
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);
}
if(r == NULL)//新树...这应该在while循环之前 – Deepthought