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 treeLLRB
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 promotedcolour 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)

OperationTimeWhy
Searchplain BST search; drawn height black-height (no two reds in a row)
Insertdescent rotations/flips per level on the way up
Spaceone colour bit per node

πŸ“Š Exam Execution Trace & Applied Exercises

Manual Execution Trace β€” applied P6, inserts into an empty LLRB (r = red Β· bh = black-height)

StepInsertFix sequenceLLRB after2-3 mirrorbh
a40red root ⟹ black40[40]
b20none β€” red left child40(20r,_)[20 40]
c5rotR@40 βž” flip@20 βž” root black20(5,40)[20]([5],[40])
d1none20(5(1r,_),40)[20]([1 5],[40])
e32none20(5(1r,_),40(32r,_))[20]([1 5],[32 40])
f64flip@40 βž” rotL@2040(20r(5(1r,_),32),64)[20 40]([1 5],[32],[64])
g13flip@5 βž” rotR@40 βž” flip@20 βž” root black20(5(1,13),40(32,64))[20]([5]([1],[13]),[40]([32],[64]))
h5duplicate ⟹ unchangedβ€”β€”
i38rotL@3220(5(1,13),40(38(32r,_),64))[20]([5]([1],[13]),[40]([32 38],[64]))
j36rotL@32 βž” rotR@38 βž” flip@3620(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

⚠️ 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