Red-Black Tree
A Red-Black tree is a self-balancing Binary Search Tree where every node carries one extra bit of information: its color — either red or black. This color system encodes balance constraints that ensure the tree never becomes too lopsided.
Red-Black trees power most real-world implementations of ordered maps and sets: java.util.TreeMap, C++'s std::map, Linux's CFS scheduler, and nginx's timer management all use Red-Black trees internally.
The five properties
Every valid Red-Black tree must satisfy all five of these properties simultaneously:
- Every node is either red or black.
- The root is always black.
- Every leaf (null sentinel node) is black.
- If a node is red, both its children must be black. (No two consecutive red nodes on any path.)
- All paths from any node to its descendant null leaves contain the same number of black nodes. This count is called the black-height.
Properties 4 and 5 together guarantee that the longest path from root to leaf is at most twice the shortest path. This keeps the tree "approximately balanced" and ensures \(O(\log n)\) operations.
← → arrow keys to step · click elements to interact
Red-Black Tree via Language Built-ins
Insertion in a Red-Black tree
New nodes are always inserted as red. This might violate property 4 (two consecutive red nodes). The fix involves one of these cases:
- Uncle is red — Recolor parent and uncle to black, grandparent to red. Move the problem up the tree.
- Uncle is black (triangle case) — Rotate the parent to make it a line.
- Uncle is black (line case) — Rotate the grandparent and swap colors of parent and grandparent.
The recoloring and rotation cascade at most \(O(\log n)\) levels up the tree, maintaining \(O(\log n)\) total insertion cost.
Red-Black vs AVL: which to choose?
Both guarantee \(O(\log n)\) for all operations. The practical differences:
- AVL trees are more strictly balanced (height ≤ 1.44 log n vs 2 log n for RB). Lookups are slightly faster. More rotations on insert/delete.
- Red-Black trees do fewer rotations on insert and delete (at most 3 total, vs \(O(\log n)\) for AVL). Writes are faster. Used when insertions are frequent.
For most applications with mixed read/write workloads, Red-Black trees are preferred — which is why they dominate standard library implementations.
Black-height and guaranteed balance
The black-height of a tree is the number of black nodes on any path from root to leaf (not counting the root itself). Because all such paths have equal black-height (property 5), and because red nodes cannot be consecutive (property 4), the total height is bounded by 2 × black-height.