2-3 Tree

Context: FIT2004_MOC · a search tree whose nodes hold 1 or 2 keys, so it can stay perfectly balanced — the model that LLRB encodes in binary nodes, and the base case of the B-tree (FIT3155) · sibling: AVL Tree Parent Framework: Tree

Quick Revision

  • 🎯 Objective: every leaf on the same level ➔ height by construction; insert bottom-up by split promote, delete top-down by rotate / merge so the target leaf is never a 2-node.
  • 📦 Core Components: 2-node ➔ 1 key, 2 children | 3-node ➔ 2 keys, 3 children | temporary 4-node ➔ 3 keys, split as soon as the operation passes back through it.
  • ⚡ Key Constraint: keys enter and leave only at a leaf; the tree grows and shrinks at the root, never at the leaves — that is why leaves stay level.

📝 How It Works

1. Structure and Balance

  • Ordering ➔ 2-node : left right · 3-node : left middle right.
  • Balance all leaves at the same level ➔ the definition itself, not a tolerance like AVL’s .
  • Height ➔ edges from root to any leaf (a lone root has height ); the LLRB black-height matches it.
  • Search ➔ compare against keys per node, descend into the matching child.

2. Insert — Split and Promote

  • Descend to the leaf ➔ key met anywhere on the path, internal nodes included ⟹ stop, tree unchanged.
  • Leaf is a 2-node ➔ insert in order ⟹ 3-node, done.
  • Leaf is a 3-node ➔ insert in order ⟹ 4-node ⟹ split: median moves up into the parent, and become two 2-nodes.
  • Propagate ➔ the parent may now be a 4-node ⟹ split again; repeat until no 4-node remains. A root split creates a new root ⟹ height , the only way the tree gains height.

3. Delete — Top-Down, Never Enter a 2-Node

  • Invariant ➔ every node you step into holds keys, so the leaf can always lose one and a parent can always give one down. Fix each child before entering it.
  • Root first ➔ a 2-node root whose children are both 2-nodes is merged with them into a 4-node before the descent.
  • Child is a 2-node, an immediate sibling is a 3-node ➔ rotate: the sibling’s nearest key goes up into the parent, the parent’s separating key comes down into the child.
  • Child and its immediate sibling are both 2-nodes ➔ merge: child separating parent key sibling ⟹ one temporary 4-node; the parent loses a key, which the invariant says it can spare.
  • At the leaf ➔ it holds keys ⟹ remove the key.
  • On the way back up ➔ split every 4-node still standing, root included, exactly as in insert. Height shrinks only when a root merge survives to the end.
  • Scope ➔ the applied sheet examines leaf keys only; an internal key is first swapped with its predecessor or successor (sanity check).

⚖️ Complexity

(derived from the balance property)

OperationTimeWhy
Search comparisons per node height
Insertdescent at most one split per level
Deletedescent with at most one rotate/merge per level at most one split per level on the way back
  • Height bound ➔ every internal node has children and all leaves are level ⟹ ⟹ ; all 3-nodes gives the other extreme .

📊 Exam Execution Trace & Applied Exercises

Manual Execution Trace — applied P5(a)

Start: [15 51]([3 6]([0],[4],[8 12]),[19 28]([16 18],[23 25],[40]),[60]([59],[65 70]))

StepOpEventChanged part
iins 5leaf is a 2-node ➔ [3 6]([0],[4 5],[8 12])
iiins 62 ⟹ split, up into [60 65]([59],[62],[70])
iiidel 59root and are 3-nodes ✓ · target and sibling both 2-nodes ⟹ merge , parent ⟹ remove [65]([60 62],[70])
ivins 10 ⟹ up ⟹ ⟹ up ⟹ root ⟹ split, new root[15]([6]([3](…),[10]([8],[12])),[51](…))
vdel 402-node root over two 2-nodes ⟹ root merge · ✓ · target , sibling ⟹ rotate up, down ⟹ ⟹ remove ⟹ split the leftover root 4-node[19 25]([16 18],[23],[28])

Final Extracted Output: [15]([6]([3]([0],[4 5]),[10]([8],[12])),[51]([19 25]([16 18],[23],[28]),[65]([60 62],[70])))

  • Step iii ➔ the merge creates a 4-node leaf; deleting from it leaves a legal 3-node, so nothing is left to split.
  • Step v ➔ the root merge was temporary — the 4-node root survives the descent untouched and is split on exit, so the height is unchanged. Its LLRB form is Left-Leaning Red-Black Tree Practice 4.

✍️ Practice

⚠️ Common Mistakes

  • 💡 Promoting the new key instead of the median ➔ the middle of the three keys goes up, whichever was inserted.
  • 💡 Stopping after one split ➔ check the parent; a promotion into a 3-node creates another 4-node.
  • 💡 Fixing the leaf after deleting ➔ the unit’s delete is top-down: rotate or merge before entering each 2-node, starting with the root.
  • 💡 Leaving a 4-node behind ➔ after a delete, split any 4-node still on the path, including a merged root.

🧠 Active Recall