How Postgres B-Trees make index lookups O(log n), and how MongoDB and Redis apply the same structures to geospatial queries and sorted sets.
Module A-13 — Capstone: DSA in Production — Postgres B-Trees, MongoDB Geospatial, and Redis Skip Lists
What this module covers: This course closes the way it opened — tying every structure back to the stack you actually ship. The three databases you reach for daily (Postgres, MongoDB, Redis) are, under the hood, running variations of exactly the trees, hashing, and probabilistic structures built across all 41 modules of this course. This capstone makes those connections explicit.
Postgres: The B-Tree Is a BST, Engineered for Disk
Module P-5 built a binary search tree and flagged its central weakness directly: insert already-sorted data, and the tree degenerates into a straight chain, O(n) instead of O(log n) — no different from Module P-10's quicksort worst case on sorted input. PostgreSQL's default index type, the B-Tree, solves this exact problem, and does one more thing a plain in-memory BST never had to worry about: staying efficient when data lives on disk, not in RAM.
Two structural differences from Module P-5's plain BST, both deliberate: each node holds many keys, not one (commonly dozens, sized to fit one disk page efficiently — reading one node means one disk read, so packing more keys per node means fewer disk reads per search), and the tree is rebalanced on every insert and delete, guaranteeing it never degenerates the way a plain BST can — there's no insertion-order-dependent worst case here at all, unlike the vulnerability flagged explicitly in Module P-5. Searching still works by the same left-smaller/right-larger logic as a BST (Module F-11's binary search, generalized), just comparing against multiple keys per node instead of one — which is exactly why WHERE id = $1 against an indexed column costs O(log n) disk reads, while a sequential scan (no usable index) costs O(n), the identical B-Tree-vs-linear-scan distinction Module F-1 flagged in its very first module.
MongoDB: Space-Partitioning Trees for "Near Me" Queries
A B-Tree (or a plain BST) answers "what's less than / greater than this value" — a fundamentally 1-dimensional ordering question. Geospatial queries — "find every location within 5km of this point" — are inherently 2-dimensional, and a 1D ordering tree has no natural way to express "nearby in two directions at once." MongoDB's geospatial indexes (built on structures in the same family as R-trees and quad-trees) solve this by recursively partitioning space itself, rather than partitioning a single sorted axis:
A "find nearby" query walks this tree the same conceptual way Module A-4's DFS or Module P-4's tree traversal walks any other tree — but the decision at each node is "which spatial region(s) could possibly contain a point within range," not "is this value smaller or larger." This is structurally the same instinct as Module A-10's trie: a trie partitions the space of possible strings by shared prefix so a query only has to explore the relevant branch; a geospatial index partitions physical space by region so a "nearby" query only has to explore the relevant quadrants, rather than checking distance to every single stored point — an O(n) scan Module A-1's heap-based top-K reasoning would recognize immediately as the wasteful alternative being avoided.
Redis: Skip Lists Instead of Balanced Trees
Sign in to keep reading
The rest of this module is free — sign in with Google to unlock it and track your progress.
Sign in & RegisterDiscussion
0Join the discussion