This page is the drill hall for Red-Black tree insertion. The parent note explained the rules; here we hit every kind of situation an insertion can throw at you — from the trivial empty tree, through both mirror sides, both bend shapes, the recolor cascade, all the way up to a real-world map lookup and an exam twist.
Before any symbols: a Red-Black tree is a Binary Search Tree where each node also carries one bit of colour — red or black — and colours obey five rules so the tree can never grow more than twice as tall on one side as another. We repair broken rules with two tools: rotation (move nodes, keep order — see Tree Rotations) and recoloring (swap colours, move nobody).
Recall The four labels we reuse everywhere
When we insert a node z (always coloured red), we name its relatives:
z ::: the freshly inserted node, always red
p ::: its parent
g ::: its grandparent (parent of p)
u ::: its uncle (the other child of g — the sibling of p)
Every insertion that causes trouble falls into exactly one cell below. "Trouble" only ever means property 4 (a red node with a red child — "red-red"), because a red new node can never break the equal-black-count rule by itself.
#
Cell (scenario class)
Uncle colour
Shape of g–p–z
Fix
Terminates?
A
Empty tree
—
—
colour root black
yes
B
Parent is black
—
—
do nothing
yes
C
Uncle RED (left side)
red
any
recolor
loops up
D
Uncle RED cascade
red then red
any
recolor twice
loops up
E
Uncle BLACK, straight line, left-left
black/NIL
straight
recolor + 1 rotate
yes
F
Uncle BLACK, straight line, right-right (mirror of E)
black/NIL
straight
recolor + 1 rotate
yes
G
Uncle BLACK, bent, left-right (zig-zag)
black/NIL
bent
2 rotates + recolor
yes
H
Uncle BLACK, bent, right-left (mirror of G)
black/NIL
bent
2 rotates + recolor
yes
I
Real-world: `std::map` insert sequence
mixed
mixed
full sequence
yes
J
Exam twist: "which property first breaks?"
—
—
reasoning
yes
How to read the map figure below, arrow by arrow. The single orange box at the very top is where every insertion begins: you just placed a new red node z. From it three navy arrows fan out — this is question 1 ("is there a parent, and is it red?"):
The left arrow goes to the violet box "no parent → A: root black" — you inserted into an empty tree.
The middle arrow goes to "parent BLACK → B: stop" — a red child under a black parent is legal, no repair.
The right arrow goes to the magenta box "parent RED → check uncle" — the only branch that has real work.
From that magenta box, two more arrows (labelled red and black) ask question 2, "what colour is the uncle?": the red arrow drops to "uncle RED → C/D: recolor", the black arrow drops to "uncle BLACK → check shape". Finally, from that box two arrows ask question 3, "straight or bent?": one to "straight → E/F: 1 rotate", one to "bent → G/H: 2 rot". So while working any example below, find the node you are stuck on, trace these arrows top-to-bottom answering the three questions in order, and the leaf box you land on is the exact matrix cell — and the exact worked example — you need.
This is the "loops up" case: one recolor floats the problem higher, and we must fix it again. We start from a valid Red-Black tree (no rule broken) and only the insertion creates the violation.
In the figure below, the left tree is the crooked "BEFORE" (all three nodes lean left); the orange arrow is our recolor-plus-right-rotate; the right tree is the tidy "AFTER" where 20 (navy = black) sits atop two red children. Navy circles are black nodes, magenta circles are red — watch 20 change from magenta to navy as it rises.
The line bends, so a single rotation would not straighten it. We rotate twice.
The figure below is a three-panel film strip: leftmost is the bent zig-zag (note 10–20 kink); the first orange arrow is the "left-rotate at p" that straightens it into the middle panel (now a clean left-left line, exactly Example E's picture); the second orange arrow is the "recolor + right-rotate" that produces the balanced rightmost tree. Navy = black, magenta = red.
No parent → root black. Black parent → stop. Red parent + red uncle → recolor & loop up. Red parent + black uncle: straight → 1 rotate; bent → 2 rotates.
Recall Self-test
After recoloring in the red-uncle case, why might you have to repeat the fix? ::: Grandparent g becomes red; if its parent is also red, a new red-red appears one level up, so you re-check from z=g.
Bent (zig-zag) case needs how many rotations, and why the first one? ::: Two; the first rotation straightens the line so the second (the real fix) has a clean straight g–p–z to work on.
Compared with an AVL Tree, why does RB rotate less? ::: RB allows a 2× path spread, so many fixes are pure recolors (no rotation); AVL's tight height-1 balance forces rotations more often. See Big-O Analysis.