3.4.8 · HinglishTrees

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

2,341 words11 min readRead in English

3.4.8 · Coding › Trees


YEH exist kyun karta hai?


5 Properties (RULES kya hain)


Do 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, seedhi line)


Common mistakes (Steel-manned)


Recall Feynman: ek 12-saal ke bacche ko explain karo

Socho ek family tree jahan har insaan red ya black shirt pehanta hai. Rule: red-shirt wala insaan apne red-shirt wale bachche nahi rakh sakta (do reds touch nahi ho sakte). Aur chahe tum kisi bhi path se neeche chalo, tumhe utni hi black shirts pass karni padti hain. Yeh rules secretly tree ko bahut lamba aur ek taraf jhukne se rokti hain. Jab ek naya baccha join karta hai (hamesha red shirt mein) aur kisi doosre red shirt wale ke paas khada hota hai, toh family ek quick fix karti hai: ya toh sab shirts swap karte hain (recoloring) ya kuch log seats swap karte hain (rotation) taaki rules hold ho sakein. Woh tidying tree ko chota rakhti hai, isliye kisi ko bhi dhundhna super fast rehta hai.


Active-recall flashcards

Naya RB node kis color ka hota hai, aur kyun?
Red — yeh sirf property 4 (red-red) violate kar sakta hai, jo fix karna aasaan hai, aur kabhi property-5 black-height balance nahi tootne deta.
Ek Red-Black tree ki property 4 batao.
Kisi red node ka red child nahi hota (do reds adjacent nahi hote).
Ek Red-Black tree ki property 5 batao.
Har root-to-NIL path mein same number of black nodes hote hain (equal black-height).
RB height kyun hai?
Black-height wale ek subtree mein nodes hote hain, aur hai, jo deta hai .
Insert fixup ke dauran rotate ki jagah recolor kab karte ho?
Jab uncle RED ho — parent & uncle black karo, grandparent red karo, phir upar move karo.
Insert fixup ke dauran rotate kab karte ho?
Jab uncle BLACK ho (ya NIL) — locally rotate + recolor karo; yeh terminate ho jaata hai.
Left rotation kya preserve karta hai?
BST in-order ordering ( pehle aur baad mein), jabki mein structure/height change karta hai.
Fixup ke end mein root ko black kyun recolor karna padta hai?
Recoloring loop ka last step hai; root ko black force karna har path mein equally ek black add karta hai, property 2 restore karta hai bina property 5 toode.
RB tree vs AVL tree balance guarantee?
RB: longest path shortest (looser, fewer rotations). AVL: subtree heights differ karte hain (tighter, more rotations).
Kya NIL leaves black nodes mane jaate hain?
Haan — NIL sentinels real black leaves hain jo black-height count karne ke liye use hoti hain.
Insertion per rotations ki maximum kitni hoti hai?
Zyada se zyada 2 (constant) — yeh ek key reason hai ki RB updates saste hain.

Connections

  • Binary Search Tree — RB ek BST hai extra coloring invariants ke saath.
  • AVL Tree — tighter balance, more rotations; contrast trade-off.
  • Tree Rotations — shared restructuring primitive.
  • Big-O Analysis height proof.
  • 2-3-4 Tree — RB trees 2-3-4 B-trees ki isometry hain (har black node + uske red children = ek 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