It's not quite obvious to me why the algorithm (see below) to delete a node from a binary search tree, that CLRS offers, works correctly (like how do we know that the inorder arrangement of the nodes would be maintained in the correct manner ?); Is there any formal proof that could explain a bit more in depth about it? I would be really greatful if anyone could help me with this. The algorithm is as follows:
Deletion
The overall strategy for deleting a node z from a binary search tree T has three basic cases but, as we shall see, one of the cases is a bit tricky.
- If z has no children, then we simply remove it by modifying its parent to re- place z with NIL as its child.
- If z has just one child, then we elevate that child to take z’s position in the tree by modifying z’s parent to replace z by z’s child.
- If z has two children, then we find z’s successor y—which must be in z’s right subtree—and have y take z’s position in the tree. The rest of z’s original right subtree becomes y’s new right subtree, and z’s left subtree becomes y’s new left subtree. This case is the tricky one because, as we shall see, it matters whether y is z’s right child.
I have tried to prove it by analys of case by case but that seems to hard.