Binary Search Tree can't delete the root
- by Ali Zahr
Everything is working fine in this function, but the problem is that I can't delete the root, I couldn't figure out what's the bug here.I've traced the "else part" it works fine until the return, it returns the old value I don't know why. Plz Help!
node *removeNode(node *Root, int key) {
node *tmp = new node;
if(key > Root->value)
Root->right = removeNode(Root->right,key);
else if(key < Root->value)
Root->left = removeNode(Root->left, key);
else if(Root->left != NULL && Root->right != NULL) {
node *minNode = findNode(Root->right);
Root->value = minNode->value;
Root->right = removeNode(Root->right,Root->value);
}
else {
tmp = Root;
if(Root->left == NULL)
Root = Root->right;
else if(Root->right == NULL)
Root = Root->left;
delete tmp;
}
return Root;
}