Skip to the workspace
Pebellslearn by building
Balanced Trees  ›  Lesson

B-Trees, the Disk, and the 2-3 Tree

20 minA wide node costs one disk seek whether it holds 2 keys or 200 — so branching factor, not key count, is what shrinks the tree

dsa / B-Trees, the Disk, and the 2-3 Tree

Explore each scene at your own pace.

01 · Why database indexes use B-trees, not BSTs

Why database indexes use B-trees, not BSTs

B-Trees, the Disk, and the 2-3 Tree

Definition
A B-tree is a search tree whose nodes each hold many sorted keys and have many children, sized to fit one disk page, so it stays very shallow and finds a key in few disk reads. A 2-3 tree is the smallest version, with 1 or 2 keys per node.
In plain words
Finding a book is quicker if each sign in the library points to dozens of aisles instead of two. You walk past only a few signs. Disks reward this because each trip to fetch a sign is slow, but reading many entries on it costs almost nothing extra.

In short: a binary tree wastes disk reads. Every level is one more seek — a real, millisecond-scale trip to storage — so a binary tree over a million keys costs 20 seeks to find one row. A B-tree widens each node to hold many keys and many children (a "page" sized to match one disk read), so the same million keys cost 3 or 4 seeks, not 20. The 2-3 tree is the smallest useful B-tree — every node holds 1 or 2 keys and has 2 or 3 children — and it's a good place to see why branching factor matters before looking at a real, wide disk index.

A short, wide tree of fat boxes (a B-tree, 3 levels) sitting beside a tall, thin tree of small circles (a binary BST, 20 levels) over the same million keys, with a caption under each: "3 reads" and "20 reads"

Why database indexes use B-trees, not BSTs

Every real database (Postgres, MySQL, SQLite) indexes rows with a B-tree, not a binary search tree. The reason is physical: reading one disk page costs about the same whether it holds 2 keys or 200, because the expensive part is the seek, not reading the extra bytes once you're there. A wide node wastes almost nothing and saves a whole level.

python
Scene 1 of 7
Project context and further resources
You're building a piece ofQuarry — the index layer
This piece — tree_depth_comparison(): Compares a wide B-tree's depth against a binary BST's, in real disk reads.
Scenario Quarry's index layer is about to move from an in-memory binary index to a disk-backed one, and the team wants real numbers before picking a page size.
tree_depth_comparison.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
tree_depth_comparison(n, order) → dictA B-tree's depth next to a binary BST's worst and balanced depths.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.