DSAVisualiseData Structures & Algorithms Studio
Chapter 04 · search-tree, ordered-map, expression-tree

Trees & Ordered Maps

Binary Search Trees (BSTs) and balanced search structures maintain keys in sorted order, supporting dynamic predecessor, successor, range queries, and ceiling operations. Red-Black trees and 2-3 trees guarantee O(log N) height by enforcing structural balance invariants.

Key Invariants & Rules

  • In a BST, all keys in node.left are strictly less than (or <= duplicate rule) node.key, and node.right keys are strictly greater.
  • In-order traversal of a valid BST visits keys in strictly monotonic ascending order.
  • BST ceiling search branches left when node.key >= target, saving the node as candidate, else branches right.
  • A 2-3 tree maintains all leaves at the exact same depth; 3-nodes hold 2 keys and 3 children.
  • Red-Black trees represent 2-3 trees using binary nodes with red left links.

Must-Know Algorithms

BST Ceiling & PredecessorO(h) time where h is tree height

Finds smallest key >= target by candidate tracking during downward search.

BST IteratorO(1) amortized next, O(h) space

Lazy traversal using an explicit stack of left-spine ancestors.

2-3 to Red-Black Tree ConversionO(N) traversal

Translates 3-nodes into equivalent 2-node clusters with red binding edges.

Learning Path (11 Lessons)

Self-Check & Concept Review

1. What is the ceiling of key X in a Binary Search Tree?

Ceiling(X) returns the least key >= X, or null if all keys in the tree are strictly smaller than X.

2. How does a lazy BST iterator achieve O(1) amortized time per next() call?

Each node in the tree is pushed onto the stack exactly once and popped exactly once, giving 2N operations total for N elements.

3. In an Array-backed BST with soft deletion (tombstones), what happens when a deleted key is re-inserted?

Soft delete preserves topological index links; re-inserting the same key reactivates the tombstoned node without restructuring the tree.

4. What structural invariant does a 2-3 tree maintain to prevent skewing?

2-3 trees grow upward during root splits, guaranteeing that all null leaf links remain at identical depth.

5. What does a red link represent in a Left-Leaning Red-Black (LLRB) tree?

Red links are 3-node internal bindings. By convention, LLRB trees restrict red links to left children to maintain 1-to-1 equivalence with 2-3 trees.