Exercise 3 (UPDATED): removing a node with two children
There are two changes to this skeleton code, to handle the case that the
right subtree of toRemove has no left subtree. The changes are:
- added "if (leftMost->left != NULL)"
- reordered the lines marked as 6, 7, and 8
Do the following steps:
- search for the leftmost node of the right subtree
- remove the leftmost node (keep track of its children)
- place the leftmost node in the same place as the node to remove
- remove and delete the node to remove
BTNode *parent, *toRemove;
// ...
// Assume that toRemove has two children.
// Write the code that searches for the leftmost child of
// the right subtree.
BTNode *leftMost = _____________________________________________ // 1
*leftMostParent = ____________________________________ // 2
if (leftMost->left != NULL) {
while (___________________________________________) { // 3
________________________________________________ // 4
________________________________________________ // 5
}
leftMostParent->left = ___________________________ // 6
leftMost->right = __________________________________ // 7
}
leftMost->left = ____________________________________ // 8
if (parent->left == toRemove) {
___________________________ // 9
} else {
___________________________ // 10
}
toRemove->left = toRemove->right = _________________________________ // 11
delete _______________________________ // 12