AVL Tree

Context: FIT2004_MOC · the self-balancing BST — same search/insert/delete, plus a rebalance pass that caps the height at · siblings in W10: 2-3 Tree, Left-Leaning Red-Black Tree Parent Framework: Binary Search Tree (BST)

Quick Revision

  • 🎯 Objective: BST the height-balance invariant at every node ➔ height ⟹ search, insert, delete all worst case (a plain BST degrades to ).
  • 📦 Core Components: plain BST op ➔ update heights bottom-up along the path ➔ first node with ➔ trinode restructure (LL/RR single rotation, LR/RL double) ➔ keep climbing.
  • ⚡ Key Constraint: classify by walking twice toward the taller side from the imbalanced node; if the second step is a tie (only happens on delete), repeat the first direction ⟹ single rotation, never double.

📝 How It Works

1. Height and Balance Factor

  • Lecture height convention ➔ empty , leaf , node . Annotate every node by hand as h,bf (the slides’ format).
  • Balance factor ➔ · positive ⟹ left taller · negative ⟹ right taller.
  • AVL ⟺ at every node — recursive, like the BST definition (“true for each subtree as well”).
  • Convention-proof ➔ Ian’s hand heights (leaf ) are not the textbook height; applied P8 counts a lone node as height . is a difference, so it is identical under either convention.

2. Operations = BST + Rebalance

  • Search ➔ identical to BST.
  • Insert ➔ BST insert, only at a leaf.
  • Delete ➔ only at a leaf; a non-leaf key is first swapped with its in-order predecessor (biggest in left subtree) or successor (smallest in right subtree) — follow the question, be able to do both (they can trigger different rotations ➔ trace step 9).
  • Rebalance ➔ walk back up the touched path, updating stored heights; the moment a node is imbalanced, restructure it; repeat until the root (“repeat until balanced” — a delete can cascade).

3. The Four Imbalance Cases

CaseTaller child’s Fix
LLrotate right at
RRrotate left at
LRrotate left at child ⟹ LL ⟹ rotate right at
RLrotate right at child ⟹ RR ⟹ rotate left at
  • Child ➔ only arises after a delete; it is the “tie ⟹ follow the first traversal” rule, i.e. LL/RR.

4. Ian’s Way — Trinode Restructure (the PT-03 shortcut)

  1. Find ➔ the lowest node with or .
  2. Two steps down the taller side ➔ gives the other two nodes; tie on step 2 ⟹ go the same way as step 1.
  3. Median of the three keys ➔ new subtree root · smaller ➔ its left child · bigger ➔ its right child.
  4. Hang the four leftover subtrees back in in-order (left-to-right) — BST ordering is preserved automatically.
  • Why one recipe covers all four cases ➔ every case ends in the same shape (diagram right-hand side); only which node is the median changes.

⚖️ Complexity

OperationTimeWhy
Searchheight , unchanged BST walk
Insert / deletedescent bottom-up check each restructure rotations , at most one per level
Spaceone stored height per node; recursive implementation aux stack
  • Heights are stored, not recomputed ➔ recomputing from scratch, as done by hand, would cost ; updating only the path is what keeps rebalancing .

Why the Height Is — applied P8 [D]

  • Sparsest AVL tree ➔ the fewest nodes at height has subtrees of heights and (equal heights would let one subtree drop its bottom level and stay AVL), each itself sparsest ⟹ , , .
  • Closed form ➔ with ; strong induction: .
  • Fibonacci floor ➔ ; induction step .
  • Conclude ➔ any AVL tree with nodes and height has at least nodes:

⚖️ Core Decision Matrix

TreeBalance definitionNode shapeRebalancing moveDelete in FIT2004
AVL per nodebinary, height fieldrotations (trinode restructure)✅
2-3 Treeall leaves on the same level2-node / 3-nodesplit promote · merge / rotate✅
LLRBequal black edges on every root-to-leaf pathbinary, colour bitrotate colour flip❌ insert only

When It Flips: need plain binary nodes and the tightest height rule ➔ AVL. Need the simplest balance statement (perfectly level leaves) ➔ 2-3. Need the 2-3 guarantee with binary nodes and unchanged BST search code ➔ LLRB, its binary encoding.

📊 Exam Execution Trace & Applied Exercises

Manual Execution Trace — insert 50 40 30 60 70 55 35, then delete 40 30 35

StepOp ()Two stepsCase ➔ medianTree after
1–2ins 50, 40———50(40,_)
3ins 30L, LLL ➔ 40(30,50)
4ins 60———40(30,50(_,60))
5ins 70R, RRR ➔ 40(30,60(50,70))
6ins 55R to 60, L to 50RL ➔ 50(40(30,_),60(55,70))
7ins 35L to 30, R to 35LR ➔ 50(35(30,40),60(55,70))
8del 40, 30——leaves, still balanced50(35,60(55,70))
9del 35R to 60, tie ⟹ RRR ➔ 60(50(_,55),70)
  • Step 6 subtrees ➔ , (50’s left), (50’s right), — moves from under to under .
  • Step 9 tie ➔ ‘s children both have height ; going L would wrongly do a double rotation. () becomes ‘s right child.
  • Then delete 60 ➔ predecessor : 55(50,70), no imbalance · successor : 70(50(_,55),_) is LR at ➔ 55(50,70). Same tree, different work.
  • Prep P1 ➔ insert into 50(20(10,_),55(52,60)): is at while the root is only ⟹ fix , LL, median ➔ 50(10(5,20),55(52,60)). The solution’s “rotate the node 10” means rises to the subtree root.
  • Applied P1 ➔ insert into 3(2(1,_),8(5(4,7),9)): both () and () are imbalanced ⟹ fix the lower one, : L to (), R to ⟹ LR, median ➔ 3(2(1,_),7(5(4,6),8(_,9))). The root is balanced again without being touched — the reason rotations always start low.

✍️ Practice

⚠️ Common Mistakes

  • 💡 Restructuring the highest imbalanced node ➔ always fix the lowest one first, then move up.
  • 💡 Choosing the second step by key instead of height ➔ step toward the taller subtree, not toward the inserted key’s side after a delete.
  • 💡 Dropping / ➔ the middle subtrees change parent; re-hang all four in in-order and recheck heights.

🧠 Active Recall