3.4.8Trees

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

2,494 words11 min readdifficulty · medium5 backlinks

WHY does this exist at all?


The 5 Properties (WHAT the rules are)


The two repair tools: rotation & recoloring

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

Worked example 1 — Recoloring (red uncle)


Worked example 2 — Rotation + recoloring (black uncle, straight line)


Common mistakes (Steel-manned)


Recall Feynman: explain to a 12-year-old

Imagine a family tree where each person wears a red or black shirt. Rule: a red-shirt person can't have a red-shirt child (no two reds touching). And no matter which path you walk down to the bottom, you must pass the same number of black shirts. These rules secretly stop the tree from getting too tall and lopsided. When a new kid joins (always in a red shirt) and stands next to another red shirt, the family does a quick fix: either everyone swaps shirts (recoloring) or a few people swap seats (rotation) so the rules hold again. That tidying keeps the tree short, so finding anyone stays super fast.


Active-recall flashcards

What color is a newly inserted RB node, and why?
Red — it can only violate property 4 (red-red), which is easy to fix, and never breaks property-5 black-height balance.
State property 4 of a Red-Black tree.
No red node has a red child (no two reds adjacent).
State property 5 of a Red-Black tree.
Every root-to-NIL path has the same number of black nodes (equal black-height).
Why is RB height O(logn)O(\log n)?
A subtree of black-height bhbh has 2bh1\ge 2^{bh}-1 nodes, and bhh/2bh \ge h/2, giving h2log2(n+1)h \le 2\log_2(n+1).
During insert fixup, when do you recolor instead of rotate?
When the uncle is RED — recolor parent & uncle black, grandparent red, then move up.
During insert fixup, when do you rotate?
When the uncle is BLACK (or NIL) — rotate + recolor locally; this terminates.
What does a left rotation preserve?
BST in-order ordering (a<x<b<y<ca<x<b<y<c before and after), while changing structure/height in O(1)O(1).
Why must the root be recolored black at the end of fixup?
Recoloring is loop's last step; forcing root black adds one black to every path equally, restoring property 2 without breaking property 5.
RB tree vs AVL tree balance guarantee?
RB: longest path 2×\le 2\times shortest (looser, fewer rotations). AVL: subtree heights differ by 1\le 1 (tighter, more rotations).
Are NIL leaves counted as black nodes?
Yes — NIL sentinels are real black leaves used to count black-height.
What is the max number of rotations per insertion?
At most 2 (constant) — a key reason RB updates are cheap.

Connections

  • Binary Search Tree — RB is a BST with extra coloring invariants.
  • AVL Tree — tighter balance, more rotations; contrast trade-off.
  • Tree Rotations — shared O(1)O(1) restructuring primitive.
  • Big-O Analysis — the O(logn)O(\log n) height proof.
  • 2-3-4 Tree — RB trees are an isometry of 2-3-4 B-trees (each black node + its red children = a B-tree node).
  • std::map / TreeMap internals — real-world implementations.

Concept Map

worst case

motivates

too many rotations

obeys

includes

includes

combined with BH gives

forces

guarantees

repaired by

repaired by

preserves BST order, restores

fixes violations of

makes efficient

Binary Search Tree

Degrades to linked list

Red-Black Tree

AVL Tree

5 Coloring Properties

No red-red rule

Same black-height per path

2x path-length bound

Height O of log n

Rotation structural fix

Recoloring color fix

std::map, TreeMap, Linux kernel

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, Red-Black tree basically ek normal BST hi hai, bas har node pe ek color label lagaya jaata hai — red ya black — aur kuch coloring rules follow karne padte hain. In rules ka asli maqsad sirf ek hai: tree ko bahut zyada lamba aur tedha hone se rokna. Sabse important rule yaad rakho: koi bhi red node ka child red nahi ho sakta (no red-red), aur har path jo root se neeche NIL tak jaata hai usme black nodes ki count same honi chahiye (yeh black-height kehlata hai). In dono se guarantee milti hai ki sabse lamba path, sabse chhote path se zyada se zyada 2 guna hi hoga — matlab height O(logn)O(\log n).

Jab naya node insert karte ho, usko hamesha red rang do. Kyun? Kyunki red node sirf "red-red" wala easy rule tod sakta hai, lekin black-height balance kabhi nahi todta — wahi sabse mushkil property hoti hai theek karna. Ab agar red-red ban gaya, to fix karne ke liye uncle ka color dekho. Uncle red hai to sirf recolor karo (parent aur uncle ko black, grandparent ko red) — koi rotation nahi, problem upar shift ho jaati hai. Uncle black/NIL hai to rotation + recolor karo, aur yeh turant terminate ho jaata hai.

Rotation ka kaam sirf structure badalna hai bina BST ka order toda — left rotation me right child upar aa jaata hai, lekin chhota-bada wala order (a<x<b<y<ca<x<b<y<c) waisa hi rehta hai. Recoloring me koi node move nahi hota, sirf rang badalte hain.

Yeh important kyun hai? Real life me C++ ka std::map, Java ka TreeMap, aur Linux kernel — sab Red-Black tree use karte hain. AVL tree zyada strictly balanced hota hai par usme rotations zyada lagti hain; Red-Black thoda loose rehta hai par updates fast hote hain (max constant rotations per insert). Yahi trade-off interview aur real systems dono me kaam aata hai. Mantra yaad rakho: "Red uncle? Recolor. Black uncle? Bend (rotate)."

Go deeper — visual, from zero

Test yourself — Trees

Connections