AlgoFrames

Trees and Heaps

Hierarchical structures: binary trees, search trees, heaps, tries and union-find.

Trees

Tree vocabulary, binary trees, traversal orders and recursive tree problems.

Binary search trees

Ordered trees that keep search fast, and the rotations that keep them balanced.

  • BST: search, insert, min and max In a binary search tree smaller keys go left and larger keys go right. Each comparison picks a direction, so search and insert follow one…
  • BST delete; successor and predecessor Deleting from a BST has three cases: a leaf, one child, or two children replaced by the in-order successor. Find successors by going…
  • Validating a BST and the in-order property A tree is a BST only if every node fits the range allowed by all its ancestors. Watch the allowed range shrink down each path.
  • Why balance matters Inserting sorted keys turns a BST into a chain with O(n) search. Compare the tree’s height with log n to see why balance matters.
  • AVL trees and rotations An AVL tree keeps the heights of every node’s subtrees within one. Single and double rotations restore balance after an insert.
  • Red-black trees A red-black tree colours nodes red or black and enforces rules that keep it roughly balanced. Inserts recolor and rotate to fix violations.
  • Ordered sets and maps: floor, ceiling, range queries Balanced trees power ordered sets and maps that answer floor, ceiling and range queries. Watch an in-order walk visit only the range.

Heaps and priority queues

Trees stored in arrays that always hand you the smallest or largest item.

Tries and union-find

Prefix trees for words and disjoint sets for grouping connected items.