【发布时间】:2011-11-30 15:12:21
【问题描述】:
基本上我的插入函数中发生的事情是触发将节点放置在我的 bst 中根右侧的部分导致程序崩溃,我不知道为什么。插入函数如下。
node* insert(node *root, node *element)
{
// Inserting into an empty tree.
if (root == NULL)
return element;
else {
// element should be inserted to the right.
if (element->bk->key < root->bk->key) {
printf("Inserting in left position.\n");
// There is a right subtree to insert the node.
if (root->left != NULL)
root->left = insert(root->left, element);
// Place the node directly to the right of root.
else
root->left = element;
}
// element should be inserted to the left.
else {
// There is a left subtree to insert the node.
if (root->right != NULL)
root->right = insert(root->right, element);
// Place the node directly to the left of root.
else
root->right = element;
}
// Return the root pointer of the updated tree.
return root;
}
}
【问题讨论】:
-
请你修复你的代码缩进。
-
你试过在调试器中运行它吗?这会告诉你它在哪条线路上崩溃了,以及为什么。
-
在你调用这个函数之前,告诉我们你是如何初始化
element的。