Sub topic 8
Balanced binary search trees (AVL and red-black)
This chapter assumes you know Big-O notation, how a binary search tree (BST) stores keys, and what an ordered map offers: lookup, insert, delete, floor, ceiling and range views.
Why plain BSTs are not enough
A BST keeps smaller keys in the left subtree and larger keys in the right subtree. Lookup, insert and delete follow one path from the root, so each costs O(h), where h is the tree height.
The height depends on insertion order. Insert keys 1, 2, 3, 4, 5 in order and every node has only a right child. The tree becomes a linked list with h = n, and every operation costs O(n).
A balanced BST fixes this. After each insert or delete it restores a shape rule that keeps h at O(log n). The restoring step is a rotation.
Rotations
A rotation changes the parent-child links around one edge. It moves one node up and one node down, and it keeps the in-order sequence of keys unchanged. This is why a rotation never breaks the search property. It runs in O(1).
Both trees read 10, 20, 25, 30, 40 in order. Only the shape changed.
AVL trees
An AVL tree stores a balance factor at each node: the height of the left subtree minus the height of the right subtree. The rule is that every balance factor is -1, 0 or 1.
After an insert, you walk back up toward the root and update heights. If a node reaches a balance factor of 2 or -2, you fix it with one or two rotations. An insert needs at most one rotation case (a single or a double rotation). A delete may need rotations at several levels on the way up.
The rule is strict, so an AVL tree has height at most about 1.44 log2(n). Lookups are fast. Updates do more rebalancing work.
Red-black trees
A red-black tree gives each node a color and enforces these rules:
- The root is black.
- A red node has no red child.
- Every path from a node down to a missing child passes through the same number of black nodes.
Rules 2 and 3 together mean the longest path is at most twice the shortest, so h is at most 2 log2(n + 1). The tree is looser than AVL. Inserts and deletes use fewer rotations (at most two for an insert and three for a delete), plus some recoloring.
| Property | AVL | Red-black |
|---|---|---|
| Height bound | about 1.44 log2 n | about 2 log2 n |
| Lookup | slightly faster | slightly slower |
| Insert and delete | more rotations | fewer rotations |
| Typical use | read-heavy lookups | general-purpose libraries |
Both guarantee O(log n) for lookup, insert, delete, floor, ceiling, first and last. An in-order walk gives sorted iteration in O(n). A range query costs O(log n + k) for k results.
In practice
Java's TreeMap and TreeSet use red-black trees. The floorEntry call from the earlier chapter follows one root-to-node path.
import java.util.TreeMap;
public class FloorDemo {
public static void main(String[] args) {
TreeMap<Integer, String> grades = new TreeMap<>();
grades.put(60, "D");
grades.put(70, "C");
grades.put(80, "B");
grades.put(90, "A");
System.out.println(grades.floorEntry(85).getValue()); // B
}
}C++ std::map and std::set are usually red-black trees too, although the standard does not require it.
Common errors
- Assuming O(1) lookup. Tree operations cost O(log n), which is slower than a hash map on pure point lookups.
- Expecting AVL and red-black trees to have the same shape. Both are valid for the same keys, and the shapes can differ.
- Forgetting that the guarantees need a consistent comparator. A broken comparator can corrupt the tree's search order.
Exercise
You insert the keys 1 through 1000 in ascending order into a plain BST and into a red-black tree. What is the worst-case height of each, and what does this mean for one lookup?
Show answer
The plain BST has height 1000, because each key becomes the right child of the previous one, so a lookup may take 1000 steps. The red-black tree has height at most 2 log2(1001), about 20, so a lookup takes at most about 20 steps.