So Im creating Simple Binary Tree, not BST. Im having problem in deletewith 2 child.
Method for deletion is DELETE WITH COPYING.
After deleteing, when I traverse the tree, it shows run time error,
Unhandled exception at 0x008B5853 in binarytree.exe: 0xC0000005: Access violation reading location 0xFEEEFEEE.
Here is the function:
void BinaryTree<mytype>::deletewithTwoChild(BTNode<mytype> *temp) //temp is the node which is to be deleted.
{
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 father is left child of its own father then
{
leaf = leaf->left; //move leaf 1 time left
while(leaf->right!=0 && leaf->left!=0) //until leaf reaches the last node of tree which has no child
{
leaf = leaf->right; //move leaf 1 time to right
}
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
deleteWithNoChild(leaf); //finally delete the last node (which is the given node for deletion) after swap
}
/////CASE 2 (for successor)
else if(father->right==temp) //if father is right child of its own father
{
leaf = leaf->right; //move leaf 1 time right
while(leaf->right!=0 && leaf->left!=0) //until leaf reaches the last node of tree which has no child
{
leaf = leaf->left; //move leaf 1 time to right
}
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
deleteWithNoChild(leaf); //finally delete the last node (which is the given node for deletion) after swap
}
}
Data Set I m using:
30
/ \
20 80
/ / \
10 40 120
\ / \
60 100 140
/ \ / \
50 70 130 150
Im trying to delete node 80, 60, 120, 140 when the run time error pops up. Plz help :((