I am implementing a binary search tree in C ++, but the code has an error, when deleting a node with 2 children the resulting tree is not well connected, the method to delete a node with 0 or 1 child works well. This code is based in this answer: https://stackoverflow.com/a/31698320/11053027 .
Edit: For example if a add the values in this order: 17, 12, 25, and then I try delete 17 that is the root, it is deleted, so when I try to show all the elements are: 12, 25, with 25 as root. But if now I try to delete 12 it is not deleted, the problem is caused when I first deleted 17. No error message is shown.
Can you help me please. Thanks in advance. Here is the code:
Node<E> *getSuccesor(Node<E> &node) const {
auto *Succesor = node.right;
while (Succesor != nullptr) {
if (Succesor->left == nullptr) return Succesor;
Succesor = Succesor->left;
}
return Succesor;
}
void remove(Node<E> &node) {
if (&node == nullptr) return;
int nChildren = numChildren(node);
Node<E> *child;
if (nChildren == 2) {
child = getSuccesor(node);
remove(*child);
child->parent = node.parent;
child->left = node.left;
child->right = node.right;
if (&node == root)
root = child;
else {
if (&node == node.parent->right)
node.parent->right = child;
else node.parent->left = child;
}
} else {
child = (node.left != nullptr ? node.left : node.right);
if (child != nullptr)
child->parent = node.parent;
if (&node == root)
root = child;
else {
if (&node == node.parent->left)
node.parent->left = child;
else node.parent->right = child;
}
}
Size--;
}