3.4.8 · D1Trees

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

1,920 words9 min readBack to topic

This page is the workshop where we build every tool the parent topic assumes you already own. We never use a symbol before we draw it.


0. What is a "node" and a "tree" — the picture first

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

Look at the figure. The plum box on top is the root. Follow any edge downward — the lower box is a child of the upper box. The two orange boxes at the very bottom have nothing beneath them: those are leaves.


1. The BST ordering rule — why "left is smaller"

The parent note says things like " go left; go left". That sentence secretly assumes the Binary Search Tree rule. Let's earn it.

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

2. Height — the number the whole topic is fighting to control

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

Look at the two trees in the figure. Both hold the same five numbers .

  • The left tree is a "stick" — every box has one child, so the longest path uses edges: height .
  • The right tree is "bushy" — the longest path uses only edges: height .

3. Colours, and what "black-height" means

Now the new ingredient. A Red-Black tree paints each node.

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

4. Subtree — the word hiding inside every rule


5. Rotation — the pointer-surgery move, pictured

The parent's biggest assumed tool. See also Tree Rotations.

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

Follow the figure left-to-right:

  • Before: is on top; its right child hangs below-right; 's left child is the subtree .
  • What we do: rises to the top; drops to become 's left child; the orphaned subtree re-attaches as 's new right child.
  • Why order survives: the sorted line reads both before and after. We only re-hung branches, never re-sorted keys.

6. How it all feeds the topic

Node parent child leaf

Binary Search Tree order rule

Height h longest path

Red-Black Tree

Node colours red black

Black-height bh

NIL sentinel leaves

Subtree as a clump

Rotation O of 1 surgery

Big-O log n growth

Read it top-down: raw nodes give us both the BST order rule and the notion of height; colours plus NIL give black-height; rotation and subtree are the repair tools. All arrows converge on the Red-Black tree.


Equipment checklist

Cover the right side and answer aloud. If any stalls, re-read that section above.

What is the root of a tree?
The single top node that has no parent.
State the BST ordering rule in one line.
Every left-subtree key is smaller than the node; every right-subtree key is larger.
What does the height measure, and why do we care?
Edges on the longest root-to-leaf path; search cost grows with it, so we want it near .
What does answer?
Roughly how many boxes deep a perfectly bushy tree of nodes must be — the power of that produces .
What is a NIL sentinel and what colour is it?
A pretend empty leaf standing in for every missing child; it is always black and counts toward black-height.
Define black-height .
The number of black nodes on any downward path from to a NIL — the same for every such path.
What does a left rotation at do to and its right child ?
It lifts up to be the parent and drops to be 's left child, re-hanging 's old left subtree onto .
Why is a rotation ?
It only re-points a constant number of edges, never touching the interiors of the subtrees.
Why must the BST order be preserved by a rotation?
Otherwise later searches would walk the wrong way and fail to find keys.

Ready? Head back to the parent topic and the properties will read like plain English. Related deeper reading: 2-3-4 Tree (the model Red-Black trees imitate) and std::map / TreeMap internals (where they ship in real libraries).