Sub topic 11
Why Red-Black Trees Have Logarithmic Height
This chapter proves that a red-black tree with n keys has height at most 2·log₂(n+1). It assumes you know the five red-black properties from the earlier chapter, binary search tree operations, and basic induction. The proof has two steps. First, bound the number of nodes below any node in terms of its black-height. Second, use the red rule to bound the total height in terms of the black-height.
The properties we rely on
The proof uses three of the properties:
- A red node never has a red child.
- Every path from a node down to a nil leaf contains the same number of black nodes.
- The root is black.
The second rule gives each node a well-defined black-height, written bh(x). It is the number of black nodes on any path from x down to a nil leaf, not counting x itself. Nil leaves count as black, as in the validator from the testing chapter, where a nil returns 1.
Step 1: many nodes below a given black-height
Claim: a subtree rooted at x contains at least 2^bh(x) − 1 internal nodes.
Here is the proof by induction on the height of x.
- Base case. If x is a nil leaf, then bh(x) = 0. The subtree has 2⁰ − 1 = 0 internal nodes, so the claim holds.
- Inductive step. Suppose x has two children. Each child has a black-height of either bh(x) (if the child is red) or bh(x) − 1 (if the child is black). In both cases it is at least bh(x) − 1.
- By the inductive hypothesis, each child's subtree has at least 2^(bh(x)−1) − 1 internal nodes.
- Adding both subtrees and x itself gives at least 2·(2^(bh(x)−1) − 1) + 1 = 2^bh(x) − 1 nodes.
This is the same shape of argument as the AVL bound. A tree with a given black-height cannot be sparse.
Step 2: the red rule limits the height
Let h be the height of the tree. On any root-to-leaf path, no two red nodes are adjacent. So at least half the nodes on the path are black, not counting the root's role as a starting point. That gives:
bh(root) ≥ h / 2
Combining the two steps
From step 1, with n keys in the tree:
n ≥ 2^bh(root) − 1 ≥ 2^(h/2) − 1
Rearranging:
n + 1 ≥ 2^(h/2)
log₂(n + 1) ≥ h / 2
h ≤ 2·log₂(n + 1)
Search, insertion and deletion each walk one root-to-leaf path and do a constant amount of work per level, plus a bounded number of rotations. All three are therefore O(log n).
Checking the bound numerically
The next snippet computes the bound and compares it with the sparsest possible case, a path that alternates colors.
import math
def max_height(n):
"""Upper bound on red-black tree height for n keys."""
return 2 * math.log2(n + 1)
def min_nodes(bh):
"""Fewest internal nodes in a subtree with black-height bh."""
return 2 ** bh - 1
for n in (7, 1000, 1_000_000):
print(n, round(max_height(n), 1))
print(min_nodes(3)) # 7For one million keys the height is at most about 40. A perfectly balanced tree would have height about 20. The bound is looser than the AVL one, which for the same n is about 1.44·log₂(n), roughly 29.
Common mistakes
- Counting the node itself. If you include x in bh(x), the formula changes by one. Pick a convention and keep it.
- Forgetting that red children keep their parent's black-height. Only black children reduce it. This is why the inductive step says "at least bh(x) − 1".
- Assuming height is exactly 2·log₂ n. The result is an upper bound. It says nothing about how often it is reached.
Exercise
A red-black tree has black-height 4 at the root. What is the minimum number of internal nodes it can have, and what is the maximum height it can have?
Show answer
The minimum number of internal nodes is 2^4 − 1 = 15. The maximum height is 2 × 4 = 8, because the longest path alternates red and black nodes, which gives at most twice as many nodes as the black count.
Exercise
Why does the proof need the rule that a red node has no red child?
Show answer
Without it, a path could contain many red nodes in a row. Then the height would not be bounded by a multiple of the black-height, and step 2 would fail. The tree could become as deep as a plain BST with the same black-height.