Sub topic 12
AVL or red-black: choosing between them
This chapter assumes you know how AVL insertion and deletion work, the five red-black properties, and the height bounds for both trees. It compares the two designs and gives you criteria for picking one.
What both trees guarantee
Both trees keep search, insertion, and deletion at O(log n) in the worst case. They differ in how tightly they hold the height down and in how much repair work each update costs.
| Aspect | AVL tree | Red-black tree |
|---|---|---|
| Height bound | about 1.44 log2(n + 2) | at most 2 log2(n + 1) |
| Extra data per node | height or balance factor | one color bit |
| Rotations per insert | at most 1 (single or double) | at most 2 |
| Rotations per delete | up to O(log n) | at most 3 |
| Recoloring | none | O(log n) worst case, O(1) amortized |
| Lookup speed | slightly faster | slightly slower |
Shape: tighter versus looser
An AVL tree is more tightly balanced. Its worst-case height is about 1.44 log2 n, while a red-black tree can reach 2 log2 n. A lookup walks at most one root-to-leaf path, so a shorter tree means fewer comparisons.
The program below computes both worst-case heights for one million keys. The AVL figure comes from the sparsest-tree recurrence.
import math
def min_nodes(h):
"""Fewest nodes an AVL tree of height h can have."""
if h == 0:
return 0
a, b = 0, 1
for _ in range(h - 1):
a, b = b, a + b + 1
return b
def avl_max_height(n):
h = 1
while min_nodes(h + 1) <= n:
h += 1
return h
def rb_max_height(n):
return int(2 * math.log2(n + 1))
n = 1_000_000
print(avl_max_height(n), rb_max_height(n)) # 28 39These are worst cases. Random insertions give trees much shorter than either bound, and the gap between the two designs is smaller in practice than the bounds suggest.
Update cost: strict versus lazy
The AVL invariant is strict. After each update, every node must have a balance factor of -1, 0, or 1. Insertion needs at most one rotation, but deletion can trigger a rotation at every ancestor on the path back to the root.
Red-black rules are looser. A red node can hide an extra level of depth, so the tree often tolerates an update without any structural change. Recoloring is cheap, and the number of rotations per update is bounded by a small constant. The cost is that the tree can stay a little taller.
Decision criteria
Choose an AVL tree when:
- Lookups dominate, for example an index that is built once and queried many times.
- Predictable, shallow search paths matter more than update speed.
Choose a red-black tree when:
- Inserts and deletes are frequent and mixed with reads.
- You want a bounded number of rotations per update. This matters if a rotation is expensive, for example when it must hold a lock or invalidate cached pointers.
- You are following an existing ecosystem. Many standard libraries, such as Java's
TreeMapand common C++std::mapimplementations, use red-black trees.
Other factors
- Memory. A color bit can often be packed into a spare pointer bit. An AVL node needs a height field or two balance bits. The difference is small, but it adds up for tiny keys.
- Implementation effort. AVL code is shorter because it has one invariant and one repair routine. Red-black deletion with its double-black cases is the hardest part of either design.
- Concurrency. Red-black updates touch fewer nodes on average, which can reduce contention in locking schemes.
Common mistakes
- Treating "red-black is faster" as a rule. Lookup-heavy workloads often favor AVL.
- Quoting an average-case result as a worst-case guarantee. Both trees guarantee O(log n) in the worst case, but only the height bounds differ.
- Forgetting that AVL deletion can rotate at many levels, so "at most one rotation" applies to insertion only.
Exercise
A service builds a sorted index of 5 million keys once at startup, then answers millions of lookups and almost never updates it. Which tree would you pick, and why?
Show answer
An AVL tree. Updates are rare, so its extra rebalancing cost is paid almost never. Its tighter height bound (about 1.44 log2 n versus 2 log2 n) gives shorter search paths and slightly faster lookups on every query.
Exercise
Why does a red-black tree need fewer rotations than an AVL tree on a delete-heavy workload?
Show answer
A red-black deletion needs at most 3 rotations, and the rest of the repair is recoloring. An AVL deletion can need a rotation at every ancestor on the path to the root, which is O(log n) rotations.