Trees are the foundation of hierarchical data organization in computer science — from database indexes (B-trees, Red-Black trees) to file systems, compilers, and network routing. Unlike linear structures, trees enable O(log n) search, insertion, and deletion when balanced, and support complex queries over hierarchical data. This cheat sheet covers every major tree structure and algorithm, from fundamental BST operations through advanced techniques like Heavy-Light Decomposition, Centroid Decomposition, and persistent segment trees used in competitive programming and production systems.
What This Cheat Sheet Covers
This topic spans 15 focused tables and 122 indexed concepts, 116 flashcards. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
A jump-to index of every table row in this cheat sheet.
An interactive map of every table and concept in this topic.
Table 1: Binary Search Tree (BST) — Core Properties and Operations
The BST is the starting point for all search trees: a binary tree where every left-subtree key is smaller than the node and every right-subtree key is larger. Understanding its fundamental operations — and its worst-case degeneration — motivates every balanced-tree variant that follows.
| Structure | Example | Description | |
|---|---|---|---|
left.key < node.key < right.key | • Every node satisfies: all keys in left subtree < node key < all keys in right subtree • enables O(log n) search on balanced trees | ||
if key < node: go leftelif key > node: go rightelse: found | • Compare key with node • recurse left or right • O(log n) balanced, O(n) worst case (degenerate/linked-list tree). | ||
BST.insert(5) → walk to null leaf, place node | • Search for key until null leaf • insert new node there • O(log n) average, O(n) worst | ||
delete node with 2 children → replace with inorder successor | • 0 children: remove directly • 1 child: replace node with child • 2 children: replace with inorder successor (smallest in right subtree), then delete successor | ||
inorder(BST) → [1, 3, 5, 7, 9] | Visiting nodes left → root → right yields keys in sorted ascending order — a unique BST property. | ||
floor(6) on {1,3,5,7} → 5ceil(6) → 7 | • Floor: largest key ≤ query. Ceiling: smallest key ≥ query • Both O(log n) via BST traversal | ||
successor(5) in {3,5,7,9} → 7 | Inorder successor: smallest key > node (leftmost of right subtree or first ancestor where node is left child). | ||
kthSmallest(root, k=3) → 3rd inorder element | • Augment each node with subtree size • navigate left/right in O(log n) without full traversal | ||
rank(5) → number of keys < 5 | • Count nodes smaller than key • O(log n) with size-augmented BST | ||
height = max(h(left), h(right)) + 1 | • Height: edges on longest root-to-leaf path • Balanced BST height ≈ log₂ n • degenerate ≈ n | ||
Insert 1,2,3,4,5 in order → linked list | • Inserting sorted data without balancing creates O(n) height • motivates AVL/Red-Black trees |