1. The height problem
A binary search tree stores the comparison at every node, so search costs the height of the tree. Insert 10, 20, 30, 40 in order and every node goes to the right: the tree is a linked list with height n − 1, and search is O(n). The invariant is correct, but the shape is terrible.
A balanced tree adds a structural invariant that keeps h = O(log n) after every insert and delete. The invariant is enforced by rotations, not by rebuilding the tree.
2. AVL balance
An AVL tree stores a height at every node. The balance factor is the height of the left subtree minus the height of the right subtree. Every node must stay in −1, 0, or 1. After an insertion, walk back up the path and rebalance the first node that violates the rule.
The height is stored, not recomputed from the whole subtree, so the update is O(1) per node. A rotation is also O(1): it changes three pointers and updates two heights.
3. The four rotations
An imbalance has four shapes. LL and RR are fixed by a single rotation; LR and RL need a rotation on the child first, then a rotation on the node. The names describe where the inserted key landed relative to the unbalanced node.
- LL — insert into the left child's left subtree; rotate right.
- RR — insert into the right child's right subtree; rotate left.
- LR — insert into the left child's right subtree; rotate left on the child, then right on the node.
- RL — insert into the right child's left subtree; rotate right on the child, then left on the node.
Example: insert 30, 20, 10
30 is the root; 20 goes left; 10 goes left of 20. The balance factor at 30 is +2, an LL case. Rotate right at 30: 20 becomes the root, 10 stays left, 30 moves to the right.
Insert 30, 10, 20 instead: 20 ends up as the left child of 30 and the right child of 10. That is the LR case: rotate left at 10, then right at 30. The root becomes 20.
4. Red-black trees
A red-black tree stores one bit of color per node and enforces a different invariant: the root is black, red nodes cannot have red children, and every root-to-leaf path has the same number of black nodes. Those rules imply height ≤ 2 log₂(n+1), so search is still O(log n).
Red-black trees do fewer rotations than AVL trees on insertion, at the cost of a slightly taller tree. That trade-off is why many standard libraries use them for ordered maps and sets. AVL trees are more rigidly balanced and win when lookups dominate.
AVL vs red-black
- AVL — tighter height, more rotations, faster lookups, slower updates.
- Red-black — looser height, fewer rotations, good mixed workloads.
- Both — O(log n) search, insert, and delete; O(n) memory.
5. B-trees
A B-tree generalizes the idea to many keys per node. It is shorter and wider, which is exactly what a disk or database wants: one node read brings many keys and children into memory. Databases and filesystems use B-trees or B+ trees for indexes; the branching factor can be in the hundreds.
The algorithmic idea is the same: keep the tree shallow by rebalancing locally. The implementation differs because a node can hold multiple keys and split when it overflows.
6. Practice
What is the balance factor of a node with left height 3 and right height 1?
Which rotation fixes an LL imbalance?
Why does a rotation not break the BST invariant?
Why might a database choose a B-tree over an AVL tree?
7. Resources
The tree lesson covers the BST invariant and traversals; this lesson adds the structural invariant that keeps the height logarithmic. The heap lesson is the other way to keep a tree complete without sorting it.