AlgoInsight

Q&A

What is a 2-3 Tree?

By AlgoInsight Team2026-06-18algorithmsdata-structures

Overview

Binary search trees can degrade to O(n) when unbalanced. A 2-3 tree stays balanced by allowing internal nodes to temporarily hold two keys and three children, then splitting overfull nodes upward and merging underfull nodes on deletion - keeping all leaves at exactly the same level. The result is guaranteed O(log n) regardless of input order.

graph TD
    A["[10 | 20]<br/>2-key node (3-node)"] --> B["[5]<br/>2-node"]
    A --> C["[15]<br/>2-node"]
    A --> D["[25]<br/>2-node"]
    B --> L1[" 3 "]
    B --> L2[" 7 "]
    C --> L3[" 12 "]
    C --> L4[" 18 "]
    D --> L5[" 22 "]
    D --> L6[" 30 "]
    style A fill:#1e1b4b,color:#fff

Node Types

All leaves are at the same level - this is the invariant that ensures balance.

Search

Searching is like a binary search tree, but at each 3-node you compare against two keys to decide which of three subtrees to follow:

To find k in node [a | b]:
  if k < a → go left
  if a < k < b → go middle
  if k > b → go right

At most log₂ n comparisons.

Insertion

Insert into the appropriate leaf. If the leaf becomes a 4-node (3 keys), split it:

  1. The middle key moves up to the parent.
  2. The two outer keys become two separate 2-nodes (children of the parent).
  3. If the parent also overflows, split it too (propagating upward).
  4. If the root splits, a new root is created - this is how the tree grows in height.
Insert 15 into [10 | 20] (a 3-node leaf):
→ Temporary 4-node [10 | 15 | 20]
→ Split: middle key 15 becomes root, 10 and 20 become children

Deletion

Deletion is more complex, but the key idea is:

Complexity

Operation Time
Search O(log n)
Insert O(log n)
Delete O(log n)
Space O(n)

All cases - not just average. This is the point: perfect balance eliminates worst-case degradation.

Why 2-3 Trees Matter

B-trees (used in databases and file systems) generalize this idea: each node can have up to t keys (t-1 to 2t-1), matching disk block sizes for efficient I/O. Red-Black trees and AVL trees are other self-balancing approaches; RB-trees can be mapped exactly to 2-3-4 trees. Understanding 2-3 trees is the clearest path to grasping why all these balanced-tree structures work.

2-3 vs Binary Search Tree vs B-tree

BST 2-3 Tree B-Tree
Node keys 1 1–2 1 to 2t-1
Balance Not guaranteed Guaranteed Guaranteed
Search O(n) worst O(log n) O(log n)
Use Simple lookups Teaching/theory Databases, filesystems

Why This Matters in Interviews

Algorithms interviews use 2-3 trees to test balanced-BST understanding - the question probes whether you grasp why balance matters and how splitting enforces it. Key points: the 2-node/3-node structure, that all leaves are the same depth (the invariant), insertion via splitting (middle key propagates up), that the tree grows from the root, and that 2-3 trees underpin B-trees (databases) and are isomorphic to Red-Black trees. Explaining the split mechanism clearly is the differentiator.

Common Pitfalls

References

More answers