Binary search trees: deletion
Delete a key from a binary search tree so that the search tree rule still holds, in the three cases of no child, one child and two children, and give the cost as Θ(h).
It opens on
Delete 14 from this tree of nine keys, and 13, which hung below it, is cut loose. Should 13 now hang from 10 as its left child, or as its right child?
Why it matters
A collection that can grow but never shrink is half a collection, and deletion is the one operation on a search tree where the node to remove can sit in the middle, so the code must close the gap without losing a key or breaking the order.
Good to know first
- Binary search trees, search and insert: every key in a left subtree is smaller and every key in a right subtree is larger, and insert returns the node so the parent can store it
- Trees and binary trees: a node has data, left and right, and a leaf has two None children
- Tree traversals: inorder lists the left subtree, the node, then the right subtree, which for a search tree is increasing order
- Recursion and the call stack: each call waits until the call it makes returns
Step by step
No child or one child
Delete a leaf or a node with one child from a search tree by handing the node's child, or None, up to its parent, and say why the search tree rule still holds.
Delete 4, which is a leaf. The search goes 8, 3, 6, 4. At 4 the left child is None, so the call returns the right child, which is None. Node 6 stores None as its left child, and no other node moves.
A common mix-up
Deleting a node deletes the subtrees below it too.
In fact: Only the node's own key goes; its one child, with everything below that child, moves up into the place.
Two children, and the successor
Delete a node with two children by copying the key of its in-order successor into it and then deleting that key from the right subtree, tracing the code on the nodes 3 and 8.
Delete 3. The variable s starts at 6, which has a left child 4, so s moves to 4, which has no left child, and the loop stops with s.data equal to 4. The node that held 3 now holds 4. Then delete runs on the right subtree with the key 4, and that call finds a leaf, returns None, and 6 stores None as its left child.
the tree with 3 deleted
A common mix-up
The successor is the right child.
In fact: It is the smallest key of the right subtree, found by going left as far as possible; for 3 the right child is 6 but the successor is 4.
The whole call, traced, and its cost
Trace delete(root, 8) call by call, state the cost of delete as Θ(h), and say that deletion does not rebalance the tree.
So call 1 stores 14 as its right child and returns itself, the root, which now holds 10.
both calls in the trace table
A common mix-up
Deleting is always cheaper than inserting.
In fact: It first searches, and with two children it walks down twice, so it costs Θ(h) like insert, and in a chain Θ(n).
Try it
Delete 40 from the tree built by inserting 30, 20, 40, 35. Which test fires and what is 30's right child afterwards?Answer
The test for no right child, which returns 35, so 30's right child is 35
Hint: 40 has only a left child
Delete 40 from the tree built by inserting 40, 20, 60, 50, 70, 55. Which key goes into the root, and which node is 60's left child afterwards?Answer
50 goes into the root, and 60's left child is 55
Hint: From 60 go left to 50, which has no left child, and 50's right child 55 takes its place
In the tree built from 30, 20, 40, 35, 50, delete 30. How many calls does delete make?Answer
3 calls, on 30, then on 40 for the key 35, then on 35
Hint: The successor is the smallest key of the right subtree, 35
If we deleted 10 instead, it has only 14 on its right. What does the call on 10 return, and what does 8 store as its right child?Answer
It returns 14, because 10 has no left child, and 8 stores 14 as its right child, with 13 still hanging below 14.
The call that deletes the successor from the right subtree never meets a node with two children. Why not?Answer
The successor has no left child, because the loop stopped at a node whose left is None, so that call takes the first one-child test.
