3.4.8 · D2Trees

Visual walkthrough — Red-Black tree — properties, rotations + recoloring (conceptual understanding)

2,598 words12 min readBack to topic

Before we begin, three words must be earned, because the parent note used them freely and we will not.

We also need two words that describe how tall and how "black-heavy" a tree is. We define them the moment we draw them, not before.

Figure — Red-Black tree — properties, rotations + recoloring (conceptual understanding)

Everything below is a chain: many blacks ⟹ many nodes ⟹ tree can't be tall. Let's walk it.


Step 1 — Set the stage: what the coloring rules force onto a single path

WHAT. Take one root-to-NIL path and look only at it. Color its circles by the rules.

WHY. Before reasoning about the whole tree, understand the strongest constraint on the worst single path — that's what "height" measures. The whole bound is really a statement about one longest path.

PICTURE. In figure s02 the left path is all black — the shortest a path can be for a given black count. The right path alternates red, black, red, black — the longest a path can be for the same black count, because Property 4 (no red touches a red) forbids two reds in a row, so a red must be padded by a black on each side.

Figure — Red-Black tree — properties, rotations + recoloring (conceptual understanding)

Term by term: is the tallest trail; the factor is "one red allowed per black"; is how many blacks every trail must contain. Rearranged: . Keep this — it's Step 5's key.


Step 2 — The base case: the smallest possible subtree

WHAT. Ask: what is the emptiest thing a subtree can be? Just a NIL.

WHY. We are about to prove "black-height forces node-count" by induction. Induction needs a rock-bottom starting case that is obviously true. NIL is that floor.

PICTURE. Figure s03: a lone black NIL square. It has zero data nodes below and above it — it is the bottom.

Figure — Red-Black tree — properties, rotations + recoloring (conceptual understanding)

Here is the quantity we claim counts the minimum data nodes forced by a black-height. At it predicts , which is exactly right. That's our foothold.


Step 3 — The inductive step: a "black-heavy" root must be a "fat" tree

WHAT. Assume the formula holds for anything shorter. Prove it for a node with black-height , using its two children.

WHY. This is the heart. We want to show that having many blacks per path is expensive — it forces the tree to contain many nodes. A tree can't be both black-heavy and skinny.

PICTURE. Figure s04: node at top, two subtrees hanging below. Each child, whatever its color, has black-height at least : if the child is black it "uses up" one black (so its own paths have one fewer), and if the child is red it uses up none (so it keeps ). Either way, at least blacks remain below each child.

Figure — Red-Black tree — properties, rotations + recoloring (conceptual understanding)

Term by term: is the minimum nodes each child is forced to carry (one less black than ); the "" is itself; the algebra doubles it back up. Result: the same formula, one level taller. Induction complete.


Step 4 — Land the node-count law

WHAT. Apply Step 3 at the root. Let = black-height of the root.

WHY. We now have a clean law connecting the tree's black-height to how many nodes it must contain. This is one of the two ropes we'll tie together.

PICTURE. Figure s05: the whole tree, with the guaranteed "minimum fatness" written across its base — a solid block of at least that many nodes.

Figure — Red-Black tree — properties, rotations + recoloring (conceptual understanding)

Step 5 — Tie the two ropes: height is

WHAT. Combine Step 1 () with Step 4 ().

WHY. Step 1 says "tall path ⟹ many blacks." Step 4 says "many blacks ⟹ many nodes ⟹ big log." Chain them and the height gets trapped under a logarithm.

PICTURE. Figure s06: two inequalities drawn as arrows pointing at from both sides, squeezing it, with the final bound below.

Figure — Red-Black tree — properties, rotations + recoloring (conceptual understanding)

Term by term: on the left, is "how big the tree's log-size is"; in the middle, is caught between two facts; on the right, is "half the tallest path" from Step 1. Multiplying across by frees . The height can never exceed twice the log of the size. That's — done.


Step 6 — Edge & degenerate cases (never leave a gap)

WHAT. Check the tree shapes where the argument feels like it might break.

WHY. The Contract: the reader must never hit a scenario we skipped. Bounds are only trustworthy if the boundary cases obey them too.

PICTURE. Figure s07 shows all four in a row.

Figure — Red-Black tree — properties, rotations + recoloring (conceptual understanding)

The one-picture summary

Everything on this page is one arrow of implication, drawn end to end below.

Figure — Red-Black tree — properties, rotations + recoloring (conceptual understanding)

Property 4 no red-red

Step 1 h at most 2 bh

Property 5 equal blacks

Step 5 combine

Step 3 induction on children

Step 4 n at least 2 to the bh minus 1

h at most 2 log2 of n plus 1

Recall Feynman retelling — the whole walk in plain words

Picture a family tree where every person wears a red or black shirt, following two habits: a red-shirt never stands right above a red-shirt, and every straight walk down to the ground passes the same number of black shirts. First (Step 1) I look at one downward walk: since reds can't touch, the most reds I can squeeze in is one between each pair of blacks — so the walk is at most twice as long as its black-shirt count. Then (Steps 2–4) I flip it around: to fit that many black shirts on every walk, the tree is forced to be crowded — with blacks per walk, there must be at least people. Then (Step 5) I glue the two facts: many blacks make the tree crowded, and a crowded tree of people can only afford about blacks per walk — and since the height is at most twice the blacks, the height is at most about . Finally (Step 6) I check the corners: the empty tree, the single person, the all-black skinny walk, and the maximally-striped tall walk — every one obeys the ceiling. So no legal shirt-coloring can ever grow a tree taller than twice the log of its size. That is why searching one is fast, and why std::map and TreeMap are built this way instead of on a plain Binary Search Tree.


Where this connects

  • Contrast with the tighter (height-differ-by-1) balancing of the AVL Tree — RB trades a little height for fewer rotations.
  • The conclusion is exactly the Big-O Analysis guarantee that makes RB operations cheap.
  • A Red-Black tree is the binary image of a 2-3-4 Tree — each 2-3-4 node maps to a small black-with-red cluster, which is another way to see why blacks stay balanced.