我创建了 Raw BinaryTree。在这棵树中,插入不像 BST,它像这样::
- 如果树为空,则添加值并使其成为根。(假设 30)
- 如果树不为空,则输入父值(30)并将新值(20)添加到其左子树。
- 如果左子树不为空,则将值(20)插入右子树。
- 对于下一次插入,再次取父亲值来确定要在哪里添加值。& 很快..
它工作正常,除非我尝试删除一个有两个孩子的节点。我用来删除的方法是deleteWithCopy。
正如我的导师所说,deletewithcopy 是: 1. 找到要删除的节点(temp)的父亲。2. 如果 temp(要删除的节点)是 'father' 的右孩子,则找到 temp 的直接后继 3. 如果 temp(要删除的节点)是 'father' 的左孩子,则找到 temp 的直接前任 4. 交换值temp 及其前任/后继 5. 删除 temp(现在是树的叶子)。
现在如何找到继任者和前任者。
Successor = 一个节点的逻辑后继是它在左子树中最右边的子节点
Predecessor = 一个节点的逻辑前驱是它在右子树中最左边的子节点
根据算法我创建了函数,但删除后,当我遍历(或打印)树时,它显示运行时错误,binarytree.exe 中 0x008B5853 处的未处理异常:0xC0000005:访问冲突读取位置 0xFEEEFEEE。这是“0xFEEEFEEE 用于标记 Visual C++ 中释放的内存”的错误。
我一次又一次地试运行了这件事,我试图访问的内存中没有任何越界的东西,我已经修复了每一个松散的结局,但仍然:(
这是功能:
void BinaryTree<mytype>::deletewithTwoChild(BTNode<mytype> *temp)
{
BTNode<mytype> *father = findfather(temp, root); //found address of father of temp node & stored it in pointer
BTNode<mytype> *leaf = temp; //created a copy of temp node
/////CASE 1 (for predecessor)
if(temp==root || father->left==temp) //if temp is left child of its father then
{
leaf = leaf->left; //move leaf 1 time left
while(leaf->right!=0 ) //until leaf reaches the right most node of left subtree
{
leaf = leaf->right; //move leaf 1 time to right
}
//swapping values
mytype var = leaf->key_value; //created a template variable to store leaf's key
leaf->key_value = temp->key_value; //assigning temp's key to leaf
temp->key_value = var; //assigning leaf's key to temp
if(leaf->right!=0) //if leaf has right child then call deletewithOneChild function
{
deletewithOneChild(leaf); //call to respective function
}
else if(leaf->left==0 && leaf->right==0) //if leaf has no children then
{
deleteWithNoChild(leaf); //call to respective function
}
}
/////CASE 2 (for successor)
else if(father->right==temp) //if temp is right child of its father, then
{
leaf = leaf->right; //move leaf 1 time right
while(leaf->left!=0) //until leaf reaches the last node of tree which has no child
{
leaf = leaf->left; //move leaf 1 time to left
}
//swapping values
mytype var = leaf->key_value; //created a template variable to store leaf's key
leaf->key_value = temp->key_value; //assigning temp's key to leaf
temp->key_value = var; //assigning leaf's key to temp
if(leaf->right!=0) //if leaf has right child then call deletewithOneChild function
{
deletewithOneChild(leaf); //call to respective function
}
else if(leaf->left==0 && leaf->right==0) //if leaf has no children then
{
deleteWithNoChild(leaf); //call to respective function
}
}
}
我正在使用的数据集:
30
/ \
20 80
/ / \
10 40 120
\ / \
60 100 140
/ \ / \
50 70 130 150
当运行时错误弹出时,我试图删除节点 80、60、120、140。请帮助 :(( 我还需要指导如何处理树 iff 30 被删除。