基本上发生的事情在我的插入功能是触发放置节点到我的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'。 – NPE