Sub topic 14
Keeping trees balanced: AVL and red-black trees
This chapter assumes you know the binary search tree (BST) ordering property, how insert and delete work, and that operation cost depends on tree height. It also assumes you know introductory Big-O notation.
Why balance matters
A plain BST costs O(h) per operation, where h is the height. Inserting sorted keys gives a chain with h = n - 1, so every operation costs O(n). A self-balancing tree adds rules that keep h at O(log n) after every insert and delete. The ordering property is unchanged. Only the shape is controlled.
Both AVL and red-black trees repair their shape with a local operation called a rotation.
Rotations
A rotation changes the parent-child links between two nodes. It keeps the in-order sequence the same, so the BST property still holds. It runs in O(1) time.
The subtree B moves from x's right side to y's left side. Every key in B is greater than x and less than y, so it fits in both positions. A left rotation is the mirror image.
def rotate_right(y):
x = y.left
y.left = x.right
x.right = y
return x # new subtree root
def rotate_left(x):
y = x.right
x.right = y.left
y.left = x
return y # new subtree rootEach function returns the new subtree root. The caller must store it in the parent link, the same way insert returns a root.
AVL trees
An AVL tree enforces a strict rule: for every node, the heights of the left and right subtrees differ by at most 1. The difference is the node's balance factor, and it must be -1, 0, or 1.
After an insert or delete, you walk back up the path and update each node's height. If a node's balance factor becomes 2 or -2, you fix it with rotations. There are four cases:
| Case | Where the extra height is | Fix |
|---|---|---|
| Left-left | Left child's left subtree | One right rotation |
| Right-right | Right child's right subtree | One left rotation |
| Left-right | Left child's right subtree | Left rotation on the child, then right rotation |
| Right-left | Right child's left subtree | Right rotation on the child, then left rotation |
The strict rule keeps the height below about 1.44 log2(n). Lookups are fast. The cost is more rotations on updates, because the tree is kept tightly balanced. An insert needs at most two rotations. A delete may need one rotation at each level on the way up.
Red-black trees
A red-black tree colors every node red or black and keeps these rules:
- The root is black.
- A red node has no red child.
- Every path from a node down to an empty position passes through the same number of black nodes.
Rules 2 and 3 together mean that the longest path is at most twice the shortest path. The height is at most 2 log2(n + 1). That is a looser bound than AVL gives.
After an insert, the new node starts red. If its parent is also red, you either recolor nodes or rotate, depending on the color of the node's uncle. Recoloring may push the problem up the tree, but an insert uses at most two rotations. A delete uses at most three.
Choosing between them
| Row | AVL | Red-black |
|---|---|---|
| Height bound | about 1.44 log2(n) | about 2 log2(n) |
| Lookup speed | Slightly faster | Slightly slower |
| Rotations per update | More | Fewer |
| Best for | Read-heavy workloads | Mixed or write-heavy workloads |
Both guarantee O(log n) for search, insert, and delete. Many standard libraries use red-black trees, for example Java's TreeMap and the ordered containers in C++ (std::map).
Exercises
Exercise
You insert 10, 20, 30 into an empty AVL tree. Which rotation runs, and what is the resulting root?
Show answer
After inserting 30, node 10 has balance factor -2 because the extra height is on its right-right side. A single left rotation at 10 runs. The root becomes 20, with children 10 and 30.
Exercise
Why does a red-black tree guarantee that no path is more than twice as long as any other?
Show answer
Every path has the same number of black nodes (rule 3). Red nodes cannot be adjacent (rule 2), so at most every other node on a path is red. The longest path therefore has at most as many red nodes as black nodes, so it is at most twice the shortest path, which has no red nodes.