Left-Leaning Red-Black Tree
Context: FIT2004_MOC Β· a 2-3 Tree drawn with ordinary binary BST nodes β a red edge glues two keys into one 3-node Β· sibling: AVL Tree Parent Framework: 2-3 Tree
Quick Revision
- π― Objective: colour each edge (stored on its child) β red edges cost no height β equal black edges on every root-to-leaf path βΉ the mirrored 2-3 tree is perfectly balanced βΉ .
- π¦ Core Components: insert as a red leaf β fix bottom-up with rotate left (red right link) Β· rotate right (two reds left-left) Β· colour flip (both children red) β root to black.
- β‘ Key Constraint: height here means black-height (black edges root β leaf), not the drawn height; check every branch has the same black count after each insert. No delete in FIT2004.
π How It Works
1. Colours and Invariants
- Colour the connecting edge β stored on the child of that edge; nodes are drawn coloured only for readability.
- No red parent with a red child β two reds in a row would be a 4-node.
- Left-leaning β a red link may only be a left child.
- Root is black β at the end of every operation, a red root is recoloured black.
- Black-height β number of black edges from root to leaf, equal on every path the 2-3 treeβs height (a lone root has black-height ). Red edges add none, which is what keeps it balanced.
2. Ianβs Way β 2-3 β· LLRB
| 2-3 tree | LLRB |
|---|---|
| 2-node | black node |
| 3-node | black (the larger key, parent) with red left child |
| temporary 4-node | black with red and red |
| split, median promoted | colour flip |
| root split, height | red root recoloured black, black-height |
- Use it to verify β convert your LLRB answer to a 2-3 tree (or vice versa) and check it matches the 2-3 insertion of the same keys.
3. Insert β Red Leaf, Then Fix Upward
- Descend as a BST, insert at a leaf, coloured red β a red edge cannot change the black-height, so only the colour invariants can break.
- Left child of a black node β fine (a 2-node became a 3-node) β panel (a).
- Right child, left sibling not red β rotate left: the right child becomes the parent and takes its colour, the old parent turns red β panel (b).
- Right child, left sibling red β colour flip: parent red, both children black β panel (c).
- Left child of a red left child β rotate right at the grandparent (Ianβs AVL trinode restructure: median on top, black, both others red), then flip β panel (d).
- Right child of a red left child β rotate left at the red parent βΉ panel (d) β panel (e). Ianβs restructure does (e)(d) in one step.
- Propagate β a flip turns the parent red, which can create a new violation one level up; keep checking to the root, then recolour the root black.

βοΈ Complexity
(derived from the 2-3 correspondence)
| Operation | Time | Why |
|---|---|---|
| Search | plain BST search; drawn height black-height (no two reds in a row) | |
| Insert | descent rotations/flips per level on the way up | |
| Space | one colour bit per node |
π Exam Execution Trace & Applied Exercises
Manual Execution Trace β applied P6, inserts into an empty LLRB (r = red Β· bh = black-height)
| Step | Insert | Fix sequence | LLRB after | 2-3 mirror | bh |
|---|---|---|---|---|---|
| a | 40 | red root βΉ black | 40 | [40] | |
| b | 20 | none β red left child | 40(20r,_) | [20 40] | |
| c | 5 | rotR@40 β flip@20 β root black | 20(5,40) | [20]([5],[40]) | |
| d | 1 | none | 20(5(1r,_),40) | [20]([1 5],[40]) | |
| e | 32 | none | 20(5(1r,_),40(32r,_)) | [20]([1 5],[32 40]) | |
| f | 64 | flip@40 β rotL@20 | 40(20r(5(1r,_),32),64) | [20 40]([1 5],[32],[64]) | |
| g | 13 | flip@5 β rotR@40 β flip@20 β root black | 20(5(1,13),40(32,64)) | [20]([5]([1],[13]),[40]([32],[64])) | |
| h | 5 | duplicate βΉ unchanged | β | β | |
| i | 38 | rotL@32 | 20(5(1,13),40(38(32r,_),64)) | [20]([5]([1],[13]),[40]([32 38],[64])) | |
| j | 36 | rotL@32 β rotR@38 β flip@36 | 20(5(1,13),40(36r(32,38),64)) | [20]([5]([1],[13]),[36 40]([32],[38],[64])) |
- bh rises only at (c) and (g) β the steps where a flip pushes red into the root and it is blackened β the 2-3 root split.
- Step (f) β the flip at leaves it a red right child of βΉ rotate left one level up; the 2-3 view is splitting into the root.
- Step (j) β the flip leaves a red left child of black βΉ legal, stop.
βοΈ Practice
Practice 1: Convert the 2-3 tree
[25 50]([20],[30],[55 60])to an LLRB and state its black-height.
- Hint: Each 3-node becomes its larger key, black, with the smaller as a red left child.
Answer
- LLRB β
50(25r(20,30),60(55r,_)).- Black-height β on every path (: red then black; : black then red) the 2-3 height.
- Why: One black edge per 2-3 level β red edges only join keys inside a node.
Practice 2: Insert
1, 2, 3, 4, 5, 6, 7into an empty LLRB. List the fixes and the final tree.
- Hint: Ascending keys always land as a right child.
Answer
- Fixes β
2rotL@1 Β·3flip@2, root black Β·4rotL@3 Β·5flip@4 then rotL@2 (the flip left as a red right child) Β·6rotL@5 Β·7flip@6, flip@4, root black.- Final β
4(2(1,3),6(5,7)), all black β the mirror of the 2-3 tree[4]([2]([1],[3]),[6]([5],[7])).- Why: A flip pushes red upward β the parent can become a red right link, which needs its own rotate left one level up.
Practice 3 (prep P3): On
19(9(5r(4(1r,_),7),13),36(21r(20,35),37))insert , then , then .
- Hint: After each flip, look at the node that just turned red and its sibling.
Answer
- Insert 12 β red left child of black βΉ valid, no fix.
- Insert 8 β red right child of black , no red left sibling βΉ rotate left:
8(7r,_).- Insert 15 β red right of beside red βΉ flip@13 ( red) βΉ now has red and red βΉ flip@9 βΉ is a red left child of black βΉ valid, stop.
- Final β
19(9r(5(4(1r,_),8(7r,_)),13(12,15)),36(21r(20,35),37)), black-height on every path.- Why: 2-3 mirror β makes , promoted makes , promoted makes root β two splits, two flips, no root split so the root stays black.
Practice 4 (applied P5b): Convert
[15]([6]([3]([0],[4 5]),[10]([8],[12])),[51]([19 25]([16 18],[23],[28]),[65]([60 62],[70])))to an LLRB.
- Hint: Work node by node; a 3-nodeβs left and middle children hang under the red key, its right child under the black key.
Answer
- LLRB β
15(6(3(0,5(4r,_)),10(8,12)),51(25(19r(18(16r,_),23),28),65(62(60r,_),70))).- Check β black-height on every path (: black, black, red, black, red) the 2-3 height.
- Why: β black , red left ; takes and , keeps on its right.
β οΈ Common Mistakes
- π‘ Measuring height by drawn levels β count black edges only; the drawn tree may be up to twice as tall.
- π‘ Forgetting the final root recolour β after a flip at the root it is red; set it black (black-height ).
- π‘ Putting the red key on the right of a 3-node β left-leaning: the smaller key is the red left child.
π§ Active Recall
Why is a newly inserted node always red?
Answer
- Short answer: a red edge adds no black-height, so the balance invariant cannot break β only colour rules can, and those are fixed locally.
- Why: 2-3 view β inserting red means adding the key into an existing node (2 β 3 or 3 β 4-node) rather than creating a new level; a black insert would lengthen one path and break perfect black balance.
Why is inserting as the right child of a black node a problem, but not as the left child?
Answer
- Short answer: a red right link violates left-leaning; it is still a valid 3-node, just drawn the wrong way.
- Why: One encoding per 3-node β rotate left swaps parent and child so the smaller key becomes the red left child, restoring the unique 2-3 β· LLRB mapping.