Interactive Knowledgebase

Data structures and algorithms, learned by doing.

Each topic has three parts: a short brief, a step-by-step walkthrough, and practice on random inputs where you make the algorithm's decisions yourself. The tracks below are in a suggested order, but each topic stands on its own.

Searching and search trees

Keeping keys in order so a lookup can rule out half of them at a time, and keeping it that way as keys come and go.

  1. Binary search

    Halve a sorted array with every comparison: lo, hi, mid, and the off-by-one traps.

  2. Binary search trees

    Search, insert and delete, including the in-order successor, and why the height matters.

    Builds on: binary search

  3. AVL trees

    Heights, balance factors, and the four single and double rotations that keep every node within one level.

    Builds on: binary search trees

  4. AVL deletion

    Remove as in a BST, then walk up: why balance 0 now means "keep going", and why one delete can need several rotations.

    Builds on: AVL trees

  5. Red-black trees

    Recoloring, rotations and the five fix-up cases that keep a binary search tree balanced.

    Builds on: binary search trees, rotations

  6. Red-black deletion

    When a black node disappears: the "double black" and the five cases that push it up or away.

    Builds on: red-black trees

  7. B-trees

    Many keys per node and splits that grow the tree from the top: how databases keep lookups to a few disk reads.

    Builds on: binary search trees

  8. B-tree deletion

    An underfull node borrows from a sibling through the parent, or merges with it, and the tree shrinks from the top.

    Builds on: B-trees

Hashing

Looking keys up without searching: compute where they belong.

  1. Hash tables

    Hashing with k mod m, collisions, chaining vs. linear probing, deleted markers, and resizing.

Heaps and sorting

From the simplest sort, through priority queues and three O(n log n) sorts, to beating n log n when keys are digits.

  1. Insertion sort

    The simplest sort: slide each value into a growing sorted part. Fast on nearly sorted input, O(n²) otherwise.

  2. Binary heaps

    A complete tree stored in an array: sift-up, sift-down, and why you swap with the larger child.

  3. Heapsort

    Bottom-up build-heap in O(n), then repeatedly swap the max to the end: O(n log n), in place.

    Builds on: binary heaps

  4. Quicksort

    Lomuto partitioning step by step, where the pivot lands, and why sorted input is the worst case.

  5. Merge sort

    Split in half, sort each half, merge: two pointers, a buffer, and a guaranteed O(n log n).

  6. Counting & radix sort

    Sorting without comparisons: counts, prefix sums, stable placement, and one digit per pass.

    Builds on: merge sort (stability)

Graphs

Exploring networks of nodes and edges, and finding shortest routes through them.

  1. Graph search: BFS & DFS

    A queue explores ring by ring, a stack goes deep and backs up. Same graph, different order.

  2. Topological sort

    Order tasks so every prerequisite comes first, with Kahn's algorithm, and spot cycles that make it impossible.

    Builds on: graph search

  3. Dijkstra's algorithm

    Shortest paths with edge lengths: settle the closest node, relax its edges, repeat.

    Builds on: BFS, priority queues (binary heaps)

  4. Bellman–Ford

    Shortest paths when edges can be negative: relax every edge, pass after pass, and catch negative cycles.

    Builds on: Dijkstra

  5. Floyd–Warshall

    Shortest paths between every pair at once: a table, and one more allowed stop per round.

    Builds on: Dijkstra, dynamic programming

  6. Union-find

    Disjoint sets as trees of parent pointers: find, union by rank, and path compression.

  7. Minimum spanning trees

    Kruskal's algorithm: take the lightest edge that doesn't close a cycle, checked with union-find.

    Builds on: graph search, union-find

  8. Prim's algorithm

    The other way to a minimum spanning tree: grow one tree by its lightest outgoing edge. Dijkstra's twin with a different update rule.

    Builds on: minimum spanning trees, Dijkstra

Strings

Storing words so their shared beginnings are shared, and finding a pattern without backing up.

  1. Tries

    A tree of letters: search, insert, prefix queries for autocomplete, and pruning on delete.

  2. KMP string matching

    Find a pattern while reading the text once: borders, the prefix table, and how far to slide after a mismatch.

Dynamic programming

Solving each overlapping sub-problem once, in a table.

  1. Edit distance & LCS

    Fill the table cell by cell, then trace back through it to read off the edits or the common subsequence.

  2. 0/1 knapsack

    Take it or leave it: items against capacity, why greedy by value per kg fails, and reading off what was packed.

    Builds on: edit distance & LCS

How topics are built: CONSTITUTION.md.