Skip to content
Merged
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ A complete, free course on data structures, from asymptotic analysis and pointer

**📖 Read it online: [dnakhoa.github.io/everything-data-structures](https://dnakhoa.github.io/everything-data-structures/)**

Five volumes · 31 chapters · 4 appendices · ~155,000 words · 225 exercises with worked solutions · Python, C, C++, Java and Go examples.
Five volumes · 31 chapters · 4 appendices · ~185,000 words · 241 exercises with worked solutions · Python, C, C++, Java and Go examples.

Written by [Ngoc Anh Khoa Doan](https://github.com/dnakhoa), with the editorial help of Claude.

Also by the author: **[Frontier LLM Engineering](https://github.com/dnakhoa/frontier-llm-engineering)** — a free field guide to how frontier language models are actually built, with exercises and runnable labs.
Also by the author: **[Frontier LLM Engineering](https://github.com/dnakhoa/frontier-llm-engineering)**: a free field guide to how frontier language models are actually built, with exercises and runnable labs.

---

Expand Down
104 changes: 91 additions & 13 deletions src/appendices/appendix-a-complexity-cheat-sheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ Two conventions for reading these tables:
| Stack | n/a | O(n) | O(1)★ | O(1)★ | O(n) |
| Queue | n/a | O(n) | O(1)★ | O(1)★ | O(n) |
| Deque | O(1) | O(n) | O(1)★ at either end | O(1)★ at either end | O(n) |
| Circular buffer (fixed capacity) | O(1) | O(n) | O(1) at the rear | O(1) at the front | O(capacity) |
| Bit array | O(1) | O(n/w) | O(1) set a bit | O(1) clear a bit | n bits |
| Gap buffer (text) | O(1) | O(n) | O(1) at the cursor, O(distance) to move it | O(1) at the cursor | O(n) |
| Rope (text) | O(log n) | O(n) | O(log n) anywhere | O(log n) anywhere | O(n) |
| Piece table (text, list of k pieces) | O(k) | O(n) | O(k), text itself never moves | O(k) | O(n + k) |

Where **w** = machine word size in bits.

The linked-list rows come with the caveat that makes them much less useful than they look: insertion and deletion are O(1) **only once you already hold the relevant node**. Getting there is O(n), so an insert-at-position is O(n) overall.

Expand All @@ -31,11 +38,16 @@ The linked-list rows come with the caveat that makes them much less useful than
| Treap | O(log n) expected | O(n) | O(log n) expected | O(log n) expected | O(n) |
| Skip list | O(log n) expected | O(n) | O(log n) expected | O(log n) expected | **O(n) expected** |
| B-tree / B+ tree | O(log_B n) I/Os | O(log_B n) I/Os | O(log_B n) | O(log_B n) | O(n) |
| Order-statistic tree (size-augmented balanced BST) | O(log n) select / rank | O(log n) | O(log n) | O(log n) | O(n) |
| van Emde Boas tree (integer keys < u) | O(log log u) | O(log log u) | O(log log u) | O(log log u) | O(u), or O(n log log u) hashed |
| y-fast trie (integer keys < u) | O(log log u) | O(log log u) | O(log log u)★ | O(log log u)★ | O(n) |
| Trie | O(L) | O(L) | O(L) | O(L) | O(N·A) |
| Radix / Patricia trie | O(L) | O(L) | O(L) | O(L) | O(N) |

Where **L** = key length, **N** = total characters stored across all keys, **A** = alphabet size.

On a BST of height h, successor, predecessor, floor and ceiling are O(h), and reporting every key in a range is O(h + k) for k results. A B-tree with minimum degree t has height at most log_t((n+1)/2); with t = 500, a billion keys fit in 4 levels. ([Chapter 7](../volume-1/ch07-binary-search-trees.md), [Chapter 10](../volume-1/ch10-multiway-search-trees-and-b-trees.md))

Two rows worth reading carefully. **Skip list space is O(n) expected**, not O(n log n): with promotion probability p = ½, the expected total number of nodes across all levels is 2n. The O(log n) is the expected *height*, which is a different quantity. And **splay trees** have no per-operation guarantee at all: a single access can cost O(n), and only a sequence of m operations is bounded, at O(m log n). That makes them unsuitable for latency-sensitive work regardless of their excellent amortized behavior.

## A.3 Hash-Based Structures
Expand All @@ -44,47 +56,60 @@ Two rows worth reading carefully. **Skip list space is O(n) expected**, not O(n
|-----------|-------------|----------------|--------|--------|-------|
| Hash table (chaining) | O(1) | O(n) | O(1)★ | O(1) | O(n + m) |
| Hash table (open addressing) | O(1) | O(n) | O(1)★ | O(1) | O(m) |
| Robin Hood hashing | O(1) | O(n) | O(1)★ | O(1), backward shift | O(m) |
| Swiss table | O(1) | O(n) | O(1)★ | O(1) | O(m) plus 1 control byte per slot |
| Cuckoo hashing | O(1) | **O(1) worst case** | O(1)★ expected | O(1) | O(n) |
| Perfect hashing (static) | O(1) | O(1) | n/a | n/a | O(n) |

Where **m** = number of buckets. Cuckoo hashing is the notable row: it is one of the few hash schemes with a genuine O(1) *worst-case* lookup, because a key can only live in one of two positions. Insertion pays for it, and can fail and require a full rehash.

Robin Hood hashing has the same *average* probe length as plain linear probing; what it improves is the variance, so the longest probe sequence and the cost of an unsuccessful lookup (which can stop early) drop sharply at high load. ([Chapter 12](../volume-2/ch12-hash-tables.md))

Java's `HashMap` converts a bucket to a red-black tree past 8 entries, giving O(log n) rather than O(n) in the worst case: a defense against deliberate collision flooding.

## A.4 Heaps and Priority Queues

| Structure | Find min | Insert | Delete min | Decrease key | Merge | Space |
|-----------|----------|--------|-----------|--------------|-------|-------|
| Binary heap | O(1) | O(log n) | O(log n) | O(log n) | O(n) | O(n) |
| Indexed binary heap (position map) | O(1) | O(log n) | O(log n) | O(log n), and O(1) to locate the item | O(n) | O(n) |
| d-ary heap | O(1) | O(log_d n) | O(d·log_d n) | O(log_d n) | O(n) | O(n) |
| Binomial heap | O(log n) | O(1)★ | O(log n) | O(log n) | O(log n) | O(n) |
| Fibonacci heap | O(1) | O(1) | O(log n)★ | O(1)★ | O(1) | O(n) |
| Pairing heap | O(1) | O(1) | O(log n)★ | O(log log n)★ | O(1) | O(n) |
| Pairing heap | O(1) | O(1) | O(log n)★ | o(log n)★, exact bound open | O(1) | O(n) |

Building a heap from n existing elements is **O(n)**, not O(n log n): Floyd's bottom-up heapify. This surprises people and is worth remembering.

The pairing heap's decrease-key is known to be Ω(log log n)★ (Fredman, 1999) and o(log n)★ (Pettie's 2005 bound is O(2^(2√(log log n)))★), but its exact cost is still an open problem.

Fibonacci heaps have the best bounds on this table and lose to binary heaps on most real workloads; see [Chapter 19](../volume-3/ch19-emerging-and-specialized-structures.md).

## A.5 Graphs

For a graph with V vertices and E edges:

| Operation | Adjacency list | Adjacency matrix |
|-----------|---------------|------------------|
| Space | O(V + E) | O(V²) |
| Add edge | O(1) | O(1) |
| Check edge (u,v) | O(deg(u)) | O(1) |
| Iterate neighbors of u | O(deg(u)) | O(V) |
| BFS / DFS | O(V + E) | O(V²) |
| Operation | Adjacency list | Adjacency matrix | CSR (static) |
|-----------|---------------|------------------|--------------|
| Space | O(V + E) | O(V²) | O(V + E), no pointers |
| Add edge | O(1) | O(1) | O(V + E) rebuild |
| Check edge (u,v) | O(deg(u)) | O(1) | O(log deg(u)) if sorted |
| Iterate neighbors of u | O(deg(u)) | O(V) | O(deg(u)), sequential |
| BFS / DFS | O(V + E) | O(V²) | O(V + E) |

| Algorithm | Complexity | Requires |
|-----------|-----------|----------|
| BFS / DFS | O(V + E) | n/a |
| Topological sort | O(V + E) | DAG |
| Cycle detection (directed, three-colour DFS) | O(V + E) | n/a |
| Bipartiteness check (BFS 2-colouring) | O(V + E) | n/a |
| Topological sort (Kahn or DFS) | O(V + E) | DAG; Kahn also detects cycles |
| Strongly connected components (Kosaraju, Tarjan) | O(V + E) | Directed graph |
| 0-1 BFS | O(V + E) | Edge weights 0 or 1 |
| DAG shortest paths | O(V + E) | DAG, any weights |
| Dijkstra (binary heap) | O((V + E) log V) | Non-negative weights |
| Dijkstra (Fibonacci heap) | O(E + V log V) | Non-negative weights |
| Bellman-Ford | O(V·E) | Detects negative cycles |
| Floyd-Warshall | O(V³) | All pairs |
| Floyd-Warshall | O(V³) | All pairs; negative edges but no negative cycles |
| Johnson's algorithm | O(V·E log V) | All pairs, sparse graphs, negative edges |
| Kruskal MST | O(E log E) | Union-find |
| Prim MST (binary heap) | O(E log V) | n/a |
| Union-Find (path compression + union by rank) | O(α(n)) ★ | n/a |
Expand All @@ -101,10 +126,13 @@ That last row is a common trap. Rollback requires undoing parent changes, which
| Bloom filter | O(k) | O(k) | ~1.44·log₂(1/ε)·n bits | False positives only |
| Counting Bloom filter | O(k) | O(k) | 4× a Bloom filter | False positives only |
| Cuckoo filter | O(1) | O(1)★ | ~(log₂(1/ε) + 3)·n bits | False positives; supports delete |
| HyperLogLog | O(1) | O(1) | O(log log n), ~12KB for billions | ~2% cardinality error |
| HyperLogLog | O(1) | O(1) | O(log log n) bits per register; 12 KB in the standard configuration | ~0.81% standard error with 16,384 registers (1.04/√m) |
| Roaring bitmap | O(log c) to find the container, then O(1) or O(log 4096) | O(log c), plus up to 4,096 shifted values in an array container | ≤ 16 bits per value within a container | None, exact |
| Count-Min Sketch | O(k) | O(k) | O((1/ε)·log(1/δ)) | Overestimates only |
| Skip list | O(log n) expected | O(log n) expected | O(n) expected | None, exact |

Where **c** = number of non-empty Roaring containers (at most 65,536 for 32-bit values).

A Bloom filter with 1% false-positive rate needs about 9.6 bits per element regardless of how large the elements are, which is the property that makes it useful.

## A.7 Competitive Programming Structures
Expand All @@ -125,13 +153,63 @@ A Bloom filter with 1% false-positive rate needs about 9.6 bits per element rega
| Suffix automaton | O(n) | O(m) | Incremental | O(n), ≤ 2n−1 states |
| Suffix tree | O(n) | O(m) | Static | O(n) |
| Palindromic tree (eertree) | O(n) | O(1)★ | Incremental | O(n) |
| Li Chao tree | O(n) | O(log C) | O(log C) | O(n) |
| Li Chao tree | O(n log C) by n inserts | O(log C) | O(log C) | O(n) |

Where **σ** = alphabet size, **m** = pattern length, **C** = coordinate range.

Mo's algorithm is offline and processes q queries in O((n + q)√n) *total*; the O(√n) figure is the amortized per-query share, not a bound on any single query.

## A.8 Complexity Growth Reference
## A.8 Array and Sequence Techniques

| Technique | Time | Extra space | Requires |
|-----------|------|-------------|----------|
| Binary search (lower / upper bound) | O(log n) | O(1) | Sorted array |
| Binary search on the answer | O(log R) predicate calls | O(1) | Monotone predicate over a range of size R |
| Two pointers | O(n) | O(1) | Sorted array (for pair-sum style problems) |
| Sliding window (fixed or variable) | O(n) | O(1) | Non-negative values for the variable "sum ≥ target" form |
| Prefix sums (1-D) | O(n) build, O(1) range sum | O(n) | Static array |
| Prefix sums (2-D) | O(rc) build, O(1) rectangle sum | O(rc) | Static r × c matrix |
| Difference array | O(1) per range add, O(n) to rebuild | O(n) | All updates before reads |
| Monotonic stack (next greater element) | O(n) total, amortized | O(n) | n/a |
| Monotonic deque (sliding-window maximum) | O(n) total, amortized | O(k) | n/a |
| In-place linked-list reversal | O(n) | O(1) | n/a |
| Floyd's cycle detection | O(μ + λ) | O(1) | n/a |
| Merge sort on a linked list | O(n log n) | O(log n) stack, O(1) bottom-up | n/a |

Where **μ** = distance to the cycle and **λ** = cycle length.

| String matching | Preprocess | Search a text of length n | Notes |
|-----------------|-----------|---------------------------|-------|
| KMP (prefix function) | O(m) | O(n) worst case | Never backs up in the text |
| Z-algorithm | O(n + m) total | O(n + m) worst case | Search P + separator + T |
| Rabin-Karp (rolling hash) | O(m) | O(n + m) expected | Random base and a large prime; verify matches to make it exact |
| Aho-Corasick (many patterns) | O(total pattern length) | O(n + number of matches) | One pass for every pattern |
| Suffix array (search) | O(n) to O(n log n) build | O(m log n) per query | Index the text once, query many patterns |

| Posting-list intersection (lists of length m ≤ n) | Time |
|---------------------------------------------------|------|
| Linear merge | O(m + n) |
| Galloping (exponential) search | O(m log(n/m)) |
| Skip pointers | Between the two, depending on skip spacing |

## A.9 Caches and Load Balancing

| Structure | Per access | Metadata per entry | Scan-resistant |
|-----------|-----------|--------------------|----------------|
| LRU (hash map + doubly linked list) | O(1) | Two pointers | No |
| LFU (frequency buckets) | O(1) | Pointers plus a count | Yes, but slow to forget |
| CLOCK (second chance) | O(1) amortized | One reference bit | No |
| ARC | O(1) | Pointers, plus ghost lists | Yes |
| W-TinyLFU | O(1) | Pointers, plus a shared Count-Min sketch | Yes |

| Load balancing rule | Maximum load, n requests into n servers |
|---------------------|-----------------------------------------|
| Uniformly random | Θ(log n / log log n) with high probability |
| Power of d choices (d ≥ 2) | ln ln n / ln d + Θ(1) with high probability |

See [Chapter 29](../volume-5/ch29-system-design-as-data-structure-composition.md) for the simulations behind both tables.

## A.10 Complexity Growth Reference

How the classes actually behave, for intuition about when each becomes infeasible:

Expand Down
2 changes: 1 addition & 1 deletion src/appendices/appendix-b-when-to-use-what.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ The structure that makes each operation cheapest, with the cost of choosing it.
| Insert in the middle given a position | Linked list, O(1) | Finding the position is O(n) |
| Merge two collections | Leftist / pairing heap, O(log n) | Slower than a binary heap otherwise |
| Membership, huge set | Bloom filter, O(k) | False positives; no enumeration |
| Count distinct | HyperLogLog, O(1) | ~2% error |
| Count distinct | HyperLogLog, O(1) | ~0.81% standard error in 12 KB |
| Connectivity under merging | Union-find, O(α(n)) | Cannot split |

## B.4 By Scale
Expand Down
Loading
Loading