7.2Deleting a Node from a Binary Search Tree
Covers IGNOU MCS-208 §7.2.3
Deletion is the hardest of the basic BST operations, because removing a node must leave a valid BST behind. There are three cases, depending on how many children the node has.
The three cases
- No children (leaf): remove it; its parent’s link becomes
NULL. - One child: the child takes the node’s place: the parent’s link now points to the child.
- Two children: find the inorder successor , the smallest key in the node’s right subtree (go right once, then left as far as possible). Copy ‘s key into the node, then delete from the right subtree. Because has no left child, deleting it is case 1 or 2.
The inorder predecessor (the largest key in the left subtree) works equally well in case 3.
Deletion follows one path to find the node and possibly one more path to find the successor: .
Key takeaways
- Case 1: leaf — just remove. Case 2: one child — child moves up.
- Case 3: two children — copy the inorder successor (min of right subtree), then delete it.
- The successor never has a left child, so its own deletion is easy.
- Deletion costs .
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Delete 30 from the BST 50(30(20, 40(35, 45)), 70) and draw the result, explaining each step.
Show worked answer
30 has two children, so find its inorder successor: go right to 40, then left to 35, which has no left child: the successor is 35. Copy 35 into the node that held 30, then delete 35 from the right subtree of that node; 35 is a leaf, so it is simply removed. Result: 50(35(20, 40(-, 45)), 70). Inorder check: 20 35 40 45 50 70 (sorted).