Binary search trees
A binary search tree stores an ordering invariant at every node. Shape controls operation cost. Unless stated otherwise, this chapter uses distinct keys, with smaller keys throughout the left subtree and larger keys throughout the right subtree.
8.1 Search, insertion and deletion
Past papers · CS 2014–2026 (23 papers): 11 questions, 14 marks
Search follows one child according to comparison with the current key. Insert into the null child position reached by an unsuccessful search. Inorder gives strictly increasing keys; descending inorder uses right, node, left. The first inserted key is root, and every ancestor must be inserted before its descendants in any insertion order giving the same tree.
| Deletion case | Replacement |
|---|---|
| Leaf | Set its parent's child link to null |
| One child | Connect its parent directly to that child |
| Two children | Copy or transplant inorder successor or predecessor, then remove that replacement node at its old location |
A successor chosen from the right subtree is its leftmost node and cannot have a left child. A predecessor chosen from the left subtree is its rightmost node and cannot have a right child. Root deletion also updates the external root pointer. If duplicate keys are permitted, state a side rule or maintain a multiplicity count.
8.2 Successors, ranges and bulk construction
Past papers · CS 2014–2026 (23 papers): 5 questions, 9 marks
If a node has a right subtree, its successor is that subtree's minimum. Otherwise ascend until reaching the first ancestor for which the path came from the left; no such ancestor means no successor. Predecessor uses the symmetric rule. If parent pointers are absent, a search from root can remember the candidate ancestor.
A search path must respect bounds from all ancestors. Comparing each child only with its own parent is insufficient to validate a whole BST. A preorder sequence can be checked or reconstructed using these inherited intervals; once traversal enters a root's right subtree it cannot later return to smaller keys in that root's left subtree.
A sorted array can form a balanced BST by choosing middles recursively in linear total time. To meld two BSTs containing nodes, obtain sorted inorder sequences, merge, then build a balanced tree: time, with linear auxiliary storage in this straightforward implementation. Repeated insertion gives a worse bound on unfavourable shapes.
For a leaf and its non-null inorder successor, a new real key strictly between their values follows the existing search path to that leaf and becomes its right child. Such intervals from different leaves are disjoint. Inserting one key in each interval therefore raises height by at most one; a one-node tree may supply no such key.
8.3 Counting insertion orders
Past papers · CS 2014–2026 (23 papers): 2 questions, 4 marks
8.4 AVL balance and rotations
Past papers · CS 2014–2026 (23 papers): 1 question, 1 mark
An AVL tree is a BST in which the two child heights differ by at most one at every node. Define balance factor as left height minus right height, using empty height .
| Imbalance after insertion | Repair at first unbalanced ancestor |
|---|---|
| Left-left | Rotate right |
| Right-right | Rotate left |
| Left-right | Rotate left at left child, then right at ancestor |
| Right-left | Rotate right at right child, then left at ancestor |
A rotation preserves inorder: for a right rotation, the left child's right subtree becomes the old root's left subtree. Update heights after links. AVL insertion repairs the first unbalanced ancestor with at most two rotations; deletion may need repair at multiple ancestors. Both have logarithmic worst-case time. Inserting additional keys into an AVL tree initially of size costs in total.
Last-minute recall
- The ordering constraint applies to entire subtrees.
- Unbalanced search trees can become chains.
- Two-child deletion uses a successor or predecessor.
- Range reporting pays for the output as well as the boundary search.
- Insertion order determines shape; inorder does not.
- Count interleavings of the two subtree orders.
- AVL rotations preserve inorder and repair local height differences.