基本上發生的事情在我的插入功能是觸發放置節點到我的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