Sub topic 11
Why Red-Black Trees Have Logarithmic Height
This chapter assumes you know the five red-black properties from the earlier chapter, basic binary search tree operations, recursion, and Big-O notation. It proves that a red-black tree with n keys has height at most 2·log₂(n+1). That bound is what guarantees O(log n) search, insertion, and deletion.
The claim
Let h be the height of a red-black tree, counted in nodes on the longest root-to-leaf path. Let n be the number of keys. The claim is:
- h ≤ 2·log₂(n + 1)
The proof has two steps. First, a lower bound on n in terms of the black-height. Second, a bound on the height in terms of the black-height, using the rule about red nodes.
The properties used are:
- The root is black.
- A red node has no red child.
- Every path from a node down to an empty (nil) leaf contains the same number of black nodes.
Nil leaves count as black, as in the validator from the testing chapter. Here the black-height bh(x) of a node x is the number of black nodes on any path from x down to a nil leaf, counting nil but not counting x itself. The convention does not change the result, only the constants you write down.
Step 1: black-height forces many nodes
Consider the subtree rooted at a node x with black-height bh(x). The claim is that this subtree holds at least 2^bh(x) − 1 internal nodes.
Prove it by induction on the height of x.
- Base case: x is a nil leaf. Its black-height is 0, and the subtree has 2⁰ − 1 = 0 internal nodes. The claim holds.
- Inductive step: x has two children. Each child is either red or black. A red child has the same black-height as x. A black child has black-height bh(x) − 1. In both cases, each child has black-height at least bh(x) − 1.
- By the induction hypothesis, each child subtree has at least 2^(bh(x)−1) − 1 internal nodes.
- Add the two subtrees and x itself: at least 2·(2^(bh(x)−1) − 1) + 1 = 2^bh(x) − 1 internal nodes.
Apply this to the root. Let b be the black-height of the root. Then:
- n ≥ 2^b − 1
Rearranged, b ≤ log₂(n + 1).
Intuitively, if you delete every red node and splice its children into its parent, you get a tree in which every leaf is at the same depth b. A tree like that is full down to depth b, so it must have at least 2^b − 1 nodes.
Step 2: red nodes at most double the height
Property 2 says no red node has a red child. So on any root-to-leaf path, red nodes can never be adjacent. At most every second node can be red.
Take the longest path from the root, with h nodes. Because the root is black and reds are never adjacent, at least half of the nodes on that path are black. That gives:
- b ≥ h / 2
Equivalently, h ≤ 2b. The worst case is a path that alternates black, red, black, red, and so on.
Combining the two steps
From step 2, h ≤ 2b. From step 1, b ≤ log₂(n + 1). Therefore:
- h ≤ 2·log₂(n + 1)
This is O(log n). Search follows a single root-to-leaf path, so it costs O(log n). Insertion and deletion walk one path to find the position, then repair the tree with recoloring and rotations. The fixup climbs at most one path back toward the root, and the earlier chapters showed that rotations are O(1). So every operation is O(log n).
Checking the bound numerically
You can compute the smallest red-black tree for a given black-height and compare it to the bound. The code below uses the formula n ≥ 2^b − 1 and the doubling rule to print the tallest possible height for several sizes.
import math
def max_rb_height(n):
"""Upper bound on red-black tree height (in nodes) for n keys."""
return 2 * math.log2(n + 1)
def min_nodes_for_black_height(b):
"""Fewest keys in a red-black tree whose root has black-height b."""
return 2 ** b - 1
for n in (1, 7, 100, 1000, 1_000_000):
print(n, round(max_rb_height(n), 1), math.floor(math.log2(n)) + 1)
print([min_nodes_for_black_height(b) for b in range(1, 6)])The first loop prints the red-black bound next to the height of a perfectly balanced tree (floor of log₂ n, plus 1). For one million keys, the bound is about 40, while a perfectly balanced tree has height 20. The final line prints [1, 3, 7, 15, 31].
Is the bound tight?
Yes, within the constant factor. Two extreme trees show the range.
- A perfectly balanced tree with every node black satisfies all properties. Its height is about log₂(n + 1). This is the best case.
- A tree whose black spine is complete, with red nodes inserted on the left side of every black node along the leftmost path, reaches height close to 2·log₂(n + 1). This is the worst case.
So the height of a red-black tree lies between log₂(n + 1) and 2·log₂(n + 1). An AVL tree is tighter: its height is at most about 1.44·log₂(n + 2), as the AVL chapter showed through Fibonacci-like growth. That is why AVL lookups can be slightly faster, while red-black trees do less rebalancing work on updates.
| Property | AVL tree | Red-black tree |
|---|---|---|
| Height bound | about 1.44·log₂ n | at most 2·log₂(n + 1) |
| What the proof counts | minimum nodes per height (Fibonacci) | minimum nodes per black-height (powers of 2) |
| Source of slack | height difference of 1 at each node | red nodes interleaved with black ones |
Common mistakes in the argument
- Forgetting that the root must be black. If the root could be red, a path of h nodes might have only (h − 1)/2 black nodes, which weakens the bound by one level. The proof is cleaner and the bound stated above holds because the root is black.
- Mixing up black-height and height. Black-height counts only black nodes. Height counts all nodes. Step 1 uses black-height, step 2 converts it to height.
- Assuming the minimum tree is all black. The all-black tree has the most nodes for its height. The tree with the fewest nodes for a given black-height is also all black, since red nodes only add keys without raising b. The height bound is about the red nodes that stretch paths, not about shrinking n.
- Skipping the nil-leaf convention. If you do not count nil leaves, your black-heights shift by one. Pick one convention and keep it throughout, including in your validator.
What the bound means in practice
A height of at most 2·log₂(n + 1) means a tree with a billion keys has height at most about 60. Each comparison during a search visits one node, so a lookup touches at most about 60 nodes. In an AVL tree it would be about 44. The difference is small, and which structure wins depends on the mix of reads and writes, a topic for the comparison chapter.
Exercise
A red-black tree has 15 keys. (a) What is the largest possible black-height of its root, using n ≥ 2^b − 1? (b) What is the largest possible height, using h ≤ 2b? Is the bound from the chapter, 2·log₂(n + 1), the same number?
Show answer
(a) From 15 ≥ 2^b − 1 we get 2^b ≤ 16, so b ≤ 4. (b) Using h ≤ 2b, h ≤ 8. The chapter's bound gives 2·log₂(16) = 8, the same number. Whether a tree with 15 keys and height 8 actually exists is a separate question. The bound is an upper limit, not a guarantee that such a tree can be built.
Exercise
Why does the rule "no red node has a red child" matter for the height bound, and what would happen to the bound if you dropped it but kept the equal black count?
Show answer
The rule lets us conclude that at least half the nodes on any path are black, so h ≤ 2b. Without it, a path could contain many consecutive red nodes between black ones. The black-height would stay small, so b ≤ log₂(n + 1) would still hold, but the height would no longer be tied to b. A tree could then be a long chain of red nodes with the same black count on every path, and the height could grow toward O(n).