謝謝,現在由於某種原因,它不能按預期工作。當我運行程序時,它只是給出了一個錯誤「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