How does a database index find one row among billions in three or four disk reads? A B-tree packs many sorted keys into each node and, when a node overflows, splits it in half and floats its median key up to the parent. The tree grows upward, never sideways — so every leaf stays the same depth and the tree stays shallow.
O(log n) search/insert · balanced · disk-friendly
many keys / node
Each node holds several sorted keys — here up to 3 — mapping to a disk page.
insert at a leaf
Every new key slots into a leaf, keeping the node's keys sorted.
split on overflow
A 4th key overflows: split in two, promote the median to the parent.
grow at the root
If the root splits, a new root appears — the whole tree deepens evenly.
btree.js — insert, overflow, split, promote
Ready
Insert keys one at a time into a B-tree with at most 3 keys per node. Press Step: each key drops into the correct leaf. When a leaf holds a 4th key it splits — the middle key rises to the parent, and if the root itself splits, the tree gains a level.
–
inserting
1
height
0
splits
How it works
A binary search tree makes one comparison per level and can grow tall and skinny. A B-tree does the opposite: it makes many comparisons per node and stays short. The magic that keeps it balanced is that the tree only ever grows at the top — splits push work upward, never leaving one branch longer than another.
1
Descend to a leaf
Follow the routing keys down: at each node, go into the child between the two keys that bracket your new key. Insertion always lands in a leaf.
2
Insert in sorted order
Slot the key into the leaf so the node's keys stay sorted. If the node now has ≤ 3 keys, you're done.
3
Overflow → split
A 4th key overflows. Split the node: left keeps the smaller keys, right takes the larger, and the median is promoted into the parent as a new routing key.
✓
Cascade or grow
The promotion may overflow the parent, splitting again up the chain. If the root splits, a fresh root holds the lone median — every leaf just got one level deeper, together.
Search
O(log n)
Insert / delete
O(log n)
Height (n keys)
O(log_t n)
Node = page
1 disk read
The code
# MAX = 2t-1 keys per node; insert returns a promotion or Nonedef insert(node, key):
if node.leaf:
sorted_put(node.keys, key)
else:
i = child_index(node, key)
up = insert(node.child[i], key)
if up: # child split — absorb its median
node.keys.insert(i, up.median)
node.child.insert(i + 1, up.right)
if len(node.keys) > MAX:
return split(node) # overflow → promote median upreturn None
def split(node):
mid = len(node.keys) // 2
right = Node(keys=node.keys[mid+1:], child=node.child[mid+1:])
median = node.keys[mid]
node.keys, node.child = node.keys[:mid], node.child[:mid+1]
return Promotion(median, right)
Because splits only ever add a node beside an existing one and push a single key upward, every leaf stays at the identical depth — that's the invariant that guarantees O(log n). Real databases tune the node size so one node fills one disk or memory page (often thousands of keys), which is why a B-tree over a billion rows is only three or four levels tall. The B+ tree variant keeps all values in linked leaves for fast range scans.
Quick check
1. When a node overflows, what happens to its median key?
Right — the median moves up to the parent, splitting the node into a left half (smaller keys) and a right half (larger keys). That upward promotion is what keeps the tree balanced.
2. Why does a B-tree stay balanced — all leaves at equal depth?
Right — insertions land in leaves and overflow propagates upward. The only way height increases is a root split, which deepens every leaf by one at once. No single branch can get ahead.
FAQ
What is a B-tree?
A balanced search tree with many sorted keys per node and many children, giving O(log n) search/insert/delete while staying only a few levels deep.
How does it stay balanced?
Inserts go into leaves; overflowing nodes split and promote their median upward. Height grows only when the root splits, so all leaves stay at equal depth.
Why do databases love B-trees?
A node maps to a disk/SSD page holding many keys, so even a billion-key index is 3–4 levels tall — a lookup is 3–4 page reads.
B-tree vs B+ tree?
A B+ tree keeps all values in linked leaves and uses internal nodes only for routing, which makes range scans fast. Most DB indexes use B+ trees.
The picture: the root splits and the tree grows from the top
Step 6 of the trace below, on the page’s own keys. The node is full at 5 keys (2t − 1 with t = 3), so it splits before 30 is inserted: three keys stay low, the median rises, and the rest form a sibling. This is the only way the tree gets taller, which is why every leaf stays at one depth.
Worked example, step by step
Minimum degree t = 3, so a node holds at most 5 keys. Insert the page’s keys 10, 20, 5, 6, 12, 30, 7, 17, then 25, 26, 27 to show a split below the root. The code splits any full node it is about to enter. The tree is printed as [keys | child child …].
#
Key
What happens
Tree after
1
10
lands in a leaf with room
[10]
2
20
lands in a leaf with room
[10 20]
3
5
lands in a leaf with room
[5 10 20]
4
6
lands in a leaf with room
[5 6 10 20]
5
12
lands in a leaf with room
[5 6 10 12 20]
6
30
root was full: split it, median moves up, height +1
[10 | [5 6] [12 20 30]]
7
7
lands in a leaf with room
[10 | [5 6 7] [12 20 30]]
8
17
lands in a leaf with room
[10 | [5 6 7] [12 17 20 30]]
9
25
lands in a leaf with room
[10 | [5 6 7] [12 17 20 25 30]]
10
26
child was full: split it on the way down, median joins the parent
[10 20 | [5 6 7] [12 17] [25 26 30]]
11
27
lands in a leaf with room
[10 20 | [5 6 7] [12 17] [25 26 27 30]]
Only 2 of the 11 inserts split anything. The first five keys pile into one root; the sixth finds it full, which is the only moment the height changes (1 to 2 levels). Keys 7 and 17 then land in leaves that have room, with no rebalancing. Key 26 shows the cheap version of a split: the right leaf was full, so 20 is promoted into a parent that had space, and no node above it is touched. Because every split happens before the descent, the parent is always known to have room for the promoted key, and no upward fix-up pass exists. After 11 keys the tree has 4 nodes and 2 levels, and a search for any key reads at most 2 nodes.
Complexity and trade-offs
Case
Time
Space
Why
B-tree search
O(logt n) node reads
O(n)
Each node holds up to 2t − 1 keys, so the tree is wide and shallow. 1,000 keys at t = 3 gave 5 to 6 levels in the runs under Common mistakes, against 10 for a balanced binary tree.
B-tree insert
O(t · logt n)
O(1) extra
One descent, with at most one split per level. Moving keys within a node costs up to t.
Balanced binary tree (AVL)
O(log₂ n)
O(n)
Fewer keys per node, so more levels: 29.9 binary levels for 10⁹ keys, each a separate memory or disk access.
Sorted array
O(log n) search, O(n) insert
O(n)
Binary search is fast, but every insert shifts the tail.
Hash table
O(1) average
O(n)
Faster point lookups, but no order: no range query, no “next key”.
The number that matters is node reads, not comparisons, because a block read from disk costs far more than a comparison in memory. With t = 500 the height bound logt((n + 1) / 2) for n = 10⁹ is 3.22, so at most 3 edges below the root: four levels. A binary tree needs about 29.9. The price is wasted space: nodes are only guaranteed to be about half full (at least t − 1 keys), and the runs under Common mistakes show where real inserts land relative to that floor.
Common mistakes
Not re-choosing the child after a split
After splitting a full child, the promoted median now sits in the parent and the key being inserted may belong to the new right sibling. Skipping the “if key is greater than the median, go right” step puts keys in the wrong subtree. Inserting 1 to 30 with t = 3 that way leaves 6 of the 30 keys unfindable by search, although all 30 are stored.
Forgetting the child pointers when an internal node splits
A leaf split moves only keys. An internal node must also hand half of its children to the new sibling. Without that, inserting 1 to 30 breaks at key 19 with a missing child, and only 9 keys remain reachable.
Never splitting the root
Every other node has a parent to take its median; the root does not. If a full root is not handled by creating a new one above it, the root just keeps growing: 30 sequential inserts leave a single node of 30 keys where the limit is 5, and lookups degrade to a scan.
Planning capacity as if nodes were full
Nodes can be as sparse as t − 1 keys. Inserting 1 to 1,000 in ascending order at t = 3 gives 497 nodes in 6 levels, with 2.01 keys per leaf out of a possible 5; 1,000 scrambled distinct keys give 312 nodes in 5 levels at 3.30 keys per leaf. Size a page budget on the half-full floor, not on n / (2t − 1).
Where this shows up
Database and file indexes
Any ordered index stored in fixed-size blocks wants wide nodes, since one block read should answer many comparisons. At t = 500 a billion keys sit at most four levels deep, so a lookup is at most four block reads.
Range queries
Because keys are kept in order, “everything between a and b” is one descent to a, then an in-order walk. The B+ variant keeps values only in linked leaves, which makes that walk a straight scan.
Ordered maps in memory
Wide nodes help in RAM too: scanning a small sorted array inside a node is friendlier to caches than chasing one pointer per key, which is why some in-memory ordered maps use B-tree nodes instead of binary ones.
Out-of-core structures
Any structure whose data outgrows memory and is read in blocks benefits from a fan-out chosen to fill one block. The height formula above, 3.22 edges for 10⁹ keys at t = 500, is the cost of a lookup in block reads.
SOLVE IT YOURSELF
Solve it: build a B-tree
A binary tree asks one question per node. On disk, one node is one page read — so you want fat nodes that answer many questions per read. A B-tree of minimum degree 3 holds a thousand keys in six levels. Python or TypeScript.
YOUR TASK
Implement BTree (minimum degree t=3) with insert(key), search(key), keys() and height(). Every non-root node must hold between t-1 and 2t-1 keys.
HINTS — 7 IDEAS
A node holds a sorted array of keys and, if internal, one more child than it has keys. Child i covers everything between key i-1 and key i.
Search scans the key array for the first key not less than the target, then descends into the matching child. Fat nodes mean very few descents.
The trick that keeps it balanced is splitting on the way down: before descending into a full child, split it. A full node has 2t-1 keys — the median moves up into the parent, the two halves become separate nodes.
Because you split before descending, the parent is guaranteed to have room for the promoted median. No cascading fix-up pass is ever needed.
The root is the exception: when the root itself is full, put a new empty root above it and split. That is the only way a B-tree grows taller, which is why all leaves stay at the same depth.
Uniform leaf depth is the whole point — every lookup costs the same number of page reads, so worst-case disk latency is predictable.
Deletion (borrowing from siblings and merging) is the harder half and is left out here; insertion carries the core idea.