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.
- Trees: root, leaf, depth, height, subtree A tree has a root, children, leaves, depth and height. Hover over a tree to see each term and measure depth with rulers.
- Binary trees and how they are stored In a binary tree each node has at most a left and a right child. See nodes as objects with references, and complete trees stored in an…
- Pre-order, in-order, post-order Pre-order, in-order and post-order visit a tree’s nodes in different orders. Watch visit numbers appear while the call stack grows beside…
- Level-order traversal Level-order traversal visits a tree one level at a time using a queue. Watch the queue fill with each level’s children.
- Recursive problems on trees: height, size, mirror, paths Height, size, mirror images and path sums all follow one pattern: solve both subtrees, then combine at the node. Watch values bubble up to…
- Serialization and construction Serialization turns a tree into a string and back. Watch tokens emitted on the way out and consumed to rebuild the same tree.
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.
- Binary heaps: array layout, sift up, sift down A binary heap is a complete tree stored in an array where every parent is smaller than its children. Sift up after insert and sift down…
- Building a heap in O(n) Heapify builds a heap from an array in O(n) by sifting down from the last internal node backward. Most nodes are near the bottom and move…
- Heap sort Heap sort builds a max-heap, then repeatedly moves the maximum to the end and shrinks the heap. It sorts in place in O(n log n).
- Priority queues in practice: top-k, k-way merge, scheduling Priority queues solve top-k, k-way merge and scheduling problems by always handing you the most urgent item next.
Tries and union-find
Prefix trees for words and disjoint sets for grouping connected items.
- Tries: insert, search, prefix A trie stores words character by character along edges, with markers where words end. Insert, search and prefix queries take time…
- Trie applications: autocomplete, word search, XOR maximum Tries power autocomplete, word search on a grid, word replacement and the maximum XOR of two numbers.
- Disjoint set union: find, union, rank, path compression Union-find tracks which items are in the same group. Parent arrows point toward a root, and path compression flattens them for…
- Union-find applications: connectivity, grouping, redundant edges Add edges one by one and union-find merges components, revealing connectivity, groups and the edge that creates a cycle.