Delete: leaf, one child, two children
After this: Delete a key in all three cases, using the next key in sorted order.
On this page · 8 sections · 4 checks
You can now find a key and add a new one. Removing a key takes more thought. An insert only adds a leaf at the bottom, but a delete can remove a node from the middle of the tree. The nodes below it still need a place, and the order rule must still hold afterwards.
First, find the node with a search. Then what you do depends on how many children it has. There are three cases, and we will start with the simplest.
Case 1: a leaf
A leaf has no children, so nothing depends on it. Remove the link from its parent, and you are done.
Take the tree of three keys and delete 30. The link from 50 to 30 is removed, and nothing else changes.
Case 2: one child
If the node has one child, that child takes its place. The parent links to the child instead.
In the tree below, 30 has one child, 40. Delete 30, and 50 links to 40 instead.
The rule still holds. 40 was already on the left of 50, so it was already smaller than 50, and it stays on that side. If 40 had nodes below it, they would move up with it, and the same reasoning would hold for them.
Case 3: two children
Now 30 has two children, 20 and 40. We cannot remove the node, because the two children would both need a new link from above, and only one link comes down to this place.
So we keep the node where it is and give it a new key. The new key has to be larger than everything on the left of the node and smaller than everything on the right.
Which key can do that? Write the keys in sorted order: 20, 30, 40. Look at the key that comes right after 30. That is 40, and it is the key we want. This next key in sorted order has a name: the . "Successor" means the one that comes next. "Inorder" refers to the inorder walk, which you met in the binary trees tutorial. A later lesson shows that on a BST this walk lists the keys in sorted order.
You can find the successor without sorting anything. When a node has a right subtree, the next key is the smallest key in that subtree, and the smallest key in a subtree is as far left as you can go. So take one step right, then go left as far as possible.
Here is the delete, step by step.
- Find the successor of 30. Step right to 40. 40 has no left child, so 40 is the successor.
- Copy the key 40 into the node that held 30.
- Remove the old 40 node. It is a leaf, so this is case 1.
This always works, for two reasons.
- The successor comes from the right subtree, so it is larger than every key on the left. It is also the smallest key in the right subtree, so it is smaller than every key that remains there. The rule still holds.
- The successor never has a left child. If it did, that child would be smaller than the successor and still larger than the deleted key, so that child would be the next key instead. So removing the old node is always case 1 or case 2.
A successor further down
In the last example the successor was the right child. It can also be further down. Delete the root, 50, from the example tree.
The keys in sorted order are 20, 30, 40, 50, 60, 70, 80, so the next key after 50 is 60.
- Find the successor. Step right to 70. Then go left to 60. 60 has no left child, so stop.
- Copy the key 60 into the root.
- Remove the old 60 node. It is a leaf, so this is case 1.
The code
This method belongs to the BST class too. It is recursive: it calls itself on a smaller part of the tree.
def delete(self, key): self.root = self._delete(self.root, key)
def _delete(self, node, key): if node is None: return None # not in the tree: nothing changes if key < node.key: node.left = self._delete(node.left, key) elif key > node.key: node.right = self._delete(node.right, key) elif node.left is None: return node.right # a leaf (None) or one child: it moves up elif node.right is None: return node.left else: succ = node.right # two children: the next key in sorted order is while succ.left is not None: # one step right, then as far left as possible succ = succ.left node.key = succ.key # copy its key... node.right = self._delete(node.right, succ.key) # ...and remove it from the right subtree return node void delete(int key) { root = delete(root, key); }
private Node delete(Node node, int key) { if (node == null) return null; // not in the tree: nothing changes if (key < node.key) { node.left = delete(node.left, key); } else if (key > node.key) { node.right = delete(node.right, key); } else if (node.left == null) { return node.right; // a leaf (null) or one child: it moves up } else if (node.right == null) { return node.left; } else { Node succ = node.right; // two children: the next key in sorted order is while (succ.left != null) succ = succ.left; // one step right, then as far left as possible node.key = succ.key; // copy its key... node.right = delete(node.right, succ.key); // ...and remove it from the right subtree } return node; }The function _deletedelete takes a node and returns the node that should now sit in that place. Here is how to read it.
- If
nodeis Nonenull, the key is not in the tree, so nothing changes. - If the key is smaller, it must be in the left subtree. The function calls itself on the left child, and stores the result as the new left child. A larger key does the same on the right.
- Otherwise this is the node to delete. If it has no left child, the function returns the right child. This covers case 2, and also case 1, because a leaf's right child is Nonenull.
- If it has no right child, the function returns the left child.
- The highlighted lines are case 3. They find the successor, copy its key, and then delete the successor from the right subtree.
The next block deletes 30 from the example tree. The left subtree of 50 looks like the tree in case 3, so the result is the same. Then the block prints the node on the left of 50, followed by that node's left child and right child.
t = BST()for k in [50, 30, 70, 20, 40, 60, 80]: t.insert(k)t.delete(30)left = t.root.leftprint(left.key, left.left.key, left.right)40 20 None
BST t = new BST();for (int k : new int[] {50, 30, 70, 20, 40, 60, 80}) t.insert(k);t.delete(30);Node left = t.root.left;System.out.println(left.key + " " + left.left.key + " " + left.right);40 20 null
Watch it happen
Here are four deletes that cover the three cases. They run in this order: 30 (two children), 20 (a leaf), 50 (the root, with two children, and a successor two steps away), then 70 (one child). Before the third delete runs, the task asks for a prediction. Tap the node you think is the inorder successor of 50.
- Delete 30Delete 30, which has two children. Watch how the next key is found: right to 40, then left as far as possible.
- Delete 20
- Delete 50
- Delete 70
Run an operation to begin.
Check yourself
Delete 30 from the example tree, using the inorder successor (the next key in sorted order). What is the left subtree of 50 afterwards?
What does this print?
t = BST()for k in [50, 30, 70, 20, 40, 60, 80]: t.insert(k)t.delete(30)left = t.root.leftprint(left.key, left.left.key, left.right)BST t = new BST();for (int k : new int[] {50, 30, 70, 20, 40, 60, 80}) t.insert(k);t.delete(30);Node left = t.root.left;System.out.println(left.key + " " + left.left.key + " " + left.right);From the example tree, delete 20 and then delete 30. Which case is the second delete, and what is the result?
Why does the inorder successor work as the replacement for a node with two children?
Connect
- Delete a leaf by removing the link to it, and a node with one child by linking its parent to that child.
- Delete a node with two children by copying the key of its inorder successor, the next key in sorted order, and removing the successor's old node.
- Find the successor: one step right, then left as far as possible.
- Say why the successor keeps the rule: it is larger than the left subtree, and smaller than the rest of the right subtree.