DSA Bible¶
A personal textbook for the data-structures & algorithms ideas that don't click the first time.
Each chapter explains one hard idea slowly: the intuition first, then the code, then an interactive demo you can step through line by line, then the usual ways people get it wrong. When something confuses me in the future, it gets a chapter here.
How to read this book: don't just read the demos, poke them. Step backwards, load weird trees, try to predict the next step before you click. The confusion goes away once you can predict what happens next.
Start here¶
1. Thinking recursively about treesWhy you should stop tracing, and the three questions that write every tree function for you.
2. Tree height, slowlyHeight from first principles, where the −1 comes from, and a step-by-step visualizer of the call stack.
3. Depth vs heightNumbers that flow down as arguments vs numbers that flow up as return values.
4. One template, many problemsSize, sum, max, leaves, min-depth (and its trap), diameter: all the same function.
5. PracticeClassic problems with hints and worked solutions.
Red-black insert, simplyInsert red, fix red-under-red, and look at the uncle. With a step-through demo that animates the rotations.
P, NP, NP-hard, NP-complete, simplyEasy to check vs easy to solve, reductions, and a map you can quiz yourself on. Three small demos.
Try it right now¶
The widget below is the main tool of this book. Press ▶ Play (or click inside it and use ← →) and watch how a tree works out its own height: nobody knows the answer at first, and the answers are passed back up from the bottom.
Colour key used in every tree demo¶
| Colour | Meaning |
|---|---|
| amber | the call that is running right now (top of the call stack) |
| indigo | a call that is paused, waiting for a child to answer |
| green | a call that has finished; its answer is in the badge |
| ∅ | an empty child (None). Calls on it still happen; that's where the base case runs. |