diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 3712eac..831138e 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -14,6 +14,8 @@ Every page on [the site](https://dnakhoa.github.io/everything-data-structures/) **Worked examples.** Concrete traces through an algorithm, with real numbers, tend to teach better than prose. +**Exercises.** Every chapter ends with a `## Exercises` section. New exercises should follow the existing format: a bold number and difficulty (`**12.8 ★★ Title.**`), the question, then a `
` block with `Solution` (and optionally one for a hint before it). Leave a blank line after `...` and before `
`, or the Markdown inside won't render; CI checks this, and checks that exercises are numbered in order. Solutions should show the working, not just the answer, and any code in them should actually run. + **Real-world usage.** If you know a production system that uses a structure in a way the book doesn't mention, that's worth adding, especially with a link to source or a design doc. ## What to check before submitting diff --git a/README.md b/README.md index 1e4e5be..9a7a062 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ 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 · ~55,000 words · Python, C, C++, Java and Go examples. +Five volumes · 31 chapters · 4 appendices · ~155,000 words · 225 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. @@ -21,7 +21,7 @@ Also by the author: **[Frontier LLM Engineering](https://github.com/dnakhoa/fron Most data structure resources stop at red-black trees. This one keeps going, through competitive-programming machinery (link-cut trees, wavelet trees, Mo's algorithm), through research-grade structures (FM-indexes, succinct trees, fractional cascading), and out the other side into system design, where the central claim of Volume V is that **a routing table is a trie, consistent hashing is a hash ring, and a message queue is a persistent FIFO**. Systems are data structures composed at scale. -Every chapter covers the same ground: the idea, the invariants, the operations with complexity analysis, working code, real-world usage, and the historical context of who invented it and why. +Every chapter covers the same ground: the idea, the invariants, the operations with complexity analysis, working code, real-world usage, and the historical context of who invented it and why. Every chapter then ends with an exercise set (★ check your understanding, ★★ apply it, ★★★ stretch), each with a folded worked solution and, for the harder ones, a hint. --- @@ -54,7 +54,7 @@ The structures that actually come up, plus the judgment to pick between them. Track 1, then: - [Chapter 8: Self-Balancing Trees](src/volume-1/ch08-self-balancing-trees.md). AVL, red-black, splay, treaps -- [Chapter 11: Graphs](src/volume-2/ch11-graphs-modeling-relationships.md). BFS/DFS, topological sort, MST, shortest paths, union-find +- [Chapter 11: Graphs](src/volume-2/ch11-graphs-modeling-relationships.md). BFS/DFS, cycle detection, topological sort, strongly connected components, union-find, MST, Dijkstra, Bellman-Ford and Floyd-Warshall - [Chapter 13: String Data Structures](src/volume-2/ch13-string-data-structures.md). Tries, suffix arrays - [Chapter 14: Probabilistic Data Structures](src/volume-3/ch14-probabilistic-data-structures.md). Bloom filters come up constantly - [Chapter 25: Complete Selection Guide](src/volume-4/ch25-complete-selection-guide-and-complexity-reference.md). The decision matrix diff --git a/book.toml b/book.toml index a1d2edc..f48a915 100644 --- a/book.toml +++ b/book.toml @@ -11,6 +11,7 @@ create-missing = false [output.html] additional-css = ["theme/custom.css"] +additional-js = ["theme/print-details.js"] default-theme = "rust" preferred-dark-theme = "navy" git-repository-url = "https://github.com/dnakhoa/everything-data-structures" diff --git a/scripts/check_book.py b/scripts/check_book.py index 0c6fbf4..578defc 100644 --- a/scripts/check_book.py +++ b/scripts/check_book.py @@ -6,6 +6,9 @@ 3. Every chapter page starts with a single H1. 4. Code fences are balanced. 5. SVG files contain no blank lines (a blank line ends the raw-HTML block in Markdown). +6. Every chapter has an Exercises section, numbered for that chapter, and every +
hint/solution block is balanced and padded with the blank lines + mdBook needs to render the Markdown inside it. """ import ast import glob @@ -100,6 +103,51 @@ def check_svgs(): print(" svgs checked") +def check_exercises(): + chapters = [p for p in iter_md() if re.match(r"ch\d+-", os.path.basename(p))] + total = 0 + for path in chapters: + num = int(re.match(r"ch(\d+)-", os.path.basename(path)).group(1)) + lines = open(path, encoding="utf-8").read().split("\n") + if "## Exercises" not in lines: + fail("%s: no '## Exercises' section" % path) + continue + found = re.findall(r"^\*\*(\d+)\.(\d+) ", "\n".join(lines), re.M) + if not found: + fail("%s: Exercises section has no numbered exercises" % path) + for ch, n in found: + if int(ch) != num: + fail("%s: exercise %s.%s is misnumbered (chapter %d)" % (path, ch, n, num)) + nums = [int(n) for _, n in found] + if nums != list(range(1, len(nums) + 1)): + fail("%s: exercises are not numbered 1..%d in order" % (path, len(nums))) + total += len(found) + + depth, in_code = 0, False + for i, ln in enumerate(lines): + if ln.lstrip().startswith("```"): + in_code = not in_code + if in_code: + continue + s = ln.strip() + if s == "
": + depth += 1 + if depth > 1: + fail("%s:%d nested
" % (path, i + 1)) + elif s == "
": + depth -= 1 + if depth < 0: + fail("%s:%d
without
" % (path, i + 1)) + if i > 0 and lines[i - 1].strip(): + fail("%s:%d needs a blank line before
" % (path, i + 1)) + elif s.startswith(""): + if i + 1 < len(lines) and lines[i + 1].strip(): + fail("%s:%d needs a blank line after " % (path, i + 1)) + if depth != 0: + fail("%s: unbalanced
blocks" % path) + print(" %d exercises in %d chapters checked" % (total, len(chapters))) + + if __name__ == "__main__": print("Checking book integrity...") check_python_blocks() @@ -107,6 +155,7 @@ def check_svgs(): check_headings() check_internal_links() check_svgs() + check_exercises() if errors: print("\n%d problem(s) found:\n" % len(errors)) diff --git a/src/images/bst-successor.svg b/src/images/bst-successor.svg new file mode 100644 index 0000000..60590ec --- /dev/null +++ b/src/images/bst-successor.svg @@ -0,0 +1,59 @@ + + + + + + + Case 1: right subtree exists + + + + + + + + + 8 + 3 + 10 + 1 + 6 + 14 + 4 + 7 + 13 + succ(3): step right to 6, then left + as far as possible. Answer: 4. + Case 2: no right subtree, walk up + + + + + + + + + 8 + 3 + 10 + 1 + 6 + 14 + 4 + 7 + 13 + succ(7): climb while arriving from a right + child (7→6→3); 3 is a left child, so stop at 8. + Dashed: the node whose successor we want. Filled: the successor. Predecessor is the mirror image. + diff --git a/src/images/btree-borrow-merge.svg b/src/images/btree-borrow-merge.svg new file mode 100644 index 0000000..1d25225 --- /dev/null +++ b/src/images/btree-borrow-merge.svg @@ -0,0 +1,72 @@ + + + + Borrow (rotation): delete 30. The child [30] has only t − 1 = 1 key. + + 20 + 40 + + + + + 10 + 30 + + 50 + 60 + + Right sibling [50 60] can spare a key. + + + + 20 + 50 + + + + + 10 + + 30 + 40 + + 60 + 40 rotates down, 50 rotates up. Then delete 30 + from the leaf, which now has a key to spare. + + Merge: then delete 10. The child [10] and its only sibling [40] are minimal. + + 20 + 50 + + + + + 10 + 40 + 60 + No sibling can spare a key. + + + 50 + + + + 10 + 20 + 40 + + 60 + The separator 20 comes down between them: + (t − 1) + 1 + (t − 1) = 2t − 1 keys, a full node. + diff --git a/src/images/dijkstra-trace.svg b/src/images/dijkstra-trace.svg new file mode 100644 index 0000000..20474cb --- /dev/null +++ b/src/images/dijkstra-trace.svg @@ -0,0 +1,40 @@ + + + + + + + + 4 + + 1 + + 2 + + 1 + + 5 + + 3 + S + dist 0 + A + dist 3 + B + dist 1 + C + dist 4 + D + dist 7 + shortest-path tree (parent pointers) + relaxed, but not on a shortest path + diff --git a/src/images/floyd-cycle.svg b/src/images/floyd-cycle.svg new file mode 100644 index 0000000..b1f59e2 --- /dev/null +++ b/src/images/floyd-cycle.svg @@ -0,0 +1,52 @@ + + + + + + + + Floyd cycle detection: tail μ = 3, cycle λ = 5 + head + + + + + + + + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + + tail: μ = 3 steps from head to the cycle + cycle start + meeting point + cycle + λ = 5 + + k − μ = 2 steps into the cycle + + μ = 3 more steps reach the start + Phase 1: slow moves 1, fast moves 2. They meet at node 6 after k = 5 steps; k is a multiple of λ. + Phase 2: restart one pointer at the head. Both move 1 step; after μ = 3 steps both are at node 4. + diff --git a/src/images/heap-array-mapping.svg b/src/images/heap-array-mapping.svg new file mode 100644 index 0000000..34586f0 --- /dev/null +++ b/src/images/heap-array-mapping.svg @@ -0,0 +1,59 @@ + + + The array is the tree read level by level, left to right + + + + + + + + + + + 90i=0 + 80i=1 + 70i=2 + 50i=3 + 40i=4 + 60i=5 + 30i=6 + 10i=7 + 20i=8 + + For i = 1 (value 80): + parent (1 − 1) / 2 = 0 + children 2·1 + 1 = 3 and 2·1 + 2 = 4 + + 90 + 80 + 70 + 50 + 40 + 60 + 30 + 10 + 20 + 012 + 345 + 678 + array + + level 0 + level 1 + level 2 + level 3 + diff --git a/src/images/path-copying.svg b/src/images/path-copying.svg index 7f70e1b..a8ce402 100644 --- a/src/images/path-copying.svg +++ b/src/images/path-copying.svg @@ -12,7 +12,7 @@ .note { fill: currentColor; font: 11px system-ui, sans-serif; opacity: .74; } .lg { fill: currentColor; font: 11px system-ui, sans-serif; } - Inserting 25 into a persistent BST. Version 1 stays valid + Inserting 35 into a persistent BST. Version 1 stays valid v1 root v2 root @@ -34,7 +34,7 @@ 30′