A B-tree is a balanced search tree whose nodes hold several keys at once — the structure behind database indexes and file systems. How it works, with step-by-step animations from the VisiGrab app.
// how b-trees work
A B-tree is a self-balancing multiway search tree. Each node stores several keys in sorted order, and an internal node with k keys has k + 1 children. Every leaf is at the same depth.
In the figure below, a node holds 20, 45 and 70. Its four children cover the keys below 20, between 20 and 45, between 45 and 70, and above 70. A child hangs from a gap, never from a key.
The B does not stand for binary. Its inventors, Rudolf Bayer and Edward McCreight (1970), never fixed its meaning.
The maximum degree m (Max. Degree) is the most children a node may have. A node holds at most one key fewer, so Max. Degree 4 means at most 3 keys per node. Every node except the root must also hold at least ⌈m/2⌉ − 1 keys; the root may hold just one.
| Max. Degree | Max keys | Min keys | Min children (internal, non-root) | Minimum degree t |
|---|---|---|---|---|
| 4 | 3 | 1 | 2 | 2 |
| 5 | 4 | 2 | 3 | none |
| 6 | 5 | 2 | 3 | 3 |
Knuth's order is the maximum number of children, as here; other texts use order for the minimum or maximum number of keys. CLRS uses a minimum degree t: a node holds t − 1 to 2t − 1 keys, so an odd Max. Degree such as 5 has no t. A B-tree of Max. Degree 4 is also called a 2-3-4 tree and corresponds to a red-black tree.
In each node, the search finds the first key not smaller than the target. If that key is the target, the search ends; otherwise it descends through the gap before that key, or past the last key. Ending at a leaf without the target means the key is absent.
A new key always goes into a leaf: the insert walks down as in a search and adds it in order. A key already in the tree is refused.
If the leaf now holds one key too many (an overflow), it splits. Its middle key, the median, moves up into the parent; the keys left of it stay, and the keys right of it form a new node. With an even number of keys, the examples on this page send the right-hand middle up: an overfull node of 4 keys (Max. Degree 4) sends its third key up, leaving two keys left and one right. The left-hand middle is equally valid.
Splits can climb towards the root. When the root splits, a new root holding only the middle key appears above the two halves. This is the only way a B-tree grows taller. CLRS instead splits full nodes on the way down.
Below, 70 goes into a tree of Max. Degree 4 whose leaf and root are both full. The leaf splits, its middle key overfills the root, and the root splits too: the tree grows a level at the top. Step through it with the arrows.
A key leaves only from a leaf. A key in an internal node first trades places with its in-order successor, the smallest key in the subtree to its right; the predecessor would work equally well.
A node other than the root may then be one key short of the minimum (an underflow). It is repaired with a neighbour: a child of the same parent directly left or right of it.
In internal nodes, children move with the keys. If a merge takes the root's last key, the empty root is dropped and the merged node becomes the root. This is the only way a B-tree gets shorter.
Below, deleting 70 leaves its leaf empty. The left neighbour has a key to spare, so 60 comes down from the parent and 50 goes up to replace it. Nothing merges.
Here every node holds its minimum. Deleting 40 leaves a leaf short, no neighbour can spare a key, and two nodes merge. The merge leaves the parent short too, so a second merge follows a level up, and the tree loses a level.
Equal leaf depth is not the result of rebalancing; it follows from where the tree grows. New keys join existing leaves, and the height changes only at the root, so all root-to-leaf paths change together.
Each operation follows one root-to-leaf path, with at most one split, borrow or merge per level, so its cost is proportional to the height. With n keys and t = ⌈m/2⌉, the fewest children of a non-root internal node, the tree has at most logt((n + 1) / 2) levels below the root. Space is linear: every node but the root holds its minimum.
| Measure | Complexity |
|---|---|
| Search | O(log n) |
| Insert | O(log n) |
| Delete | O(log n) |
| Height | O(log n) |
| Space | O(n) |
A plain binary search tree is O(log n) only when balanced; its worst case is O(n). Self-balancing binary search trees, such as the AVL tree and the red-black tree, keep their height at O(log n) by rebalancing after each insertion and deletion. A B-tree branches many ways per node instead of two, so a search visits far fewer nodes.
The figure below inserts the seven keys 10, 20, 30, 40, 50, 60 and 70, in ascending order, into both. In the BST each key becomes the right child of the previous one: a chain seven levels tall. The B-tree of Max. Degree 4 has two levels: 30 and 60 in the root, above the leaves [10 20], [40 50] and [70].
A B-tree cannot become a chain: it grows only at the root, and every non-root node keeps its minimum.
Database indexes and file systems are built on B-trees, mostly the B+ tree variant. A B+ tree keeps records only in its leaves, copies of keys in the inner nodes, and links the leaves in order for range scans. A node fills one disk page and branches hundreds of ways, so three or four levels cover millions of keys.
// in the app
In VisiGrab you build the tree yourself, and every change plays out step by step.
Add any key, tap a key to remove it, or start from a random tree. Every insert and delete is kept in a list.
Step through every descent, split, borrow and merge — and step back through them.
Up to 3, 4 or 5 keys per node. See how the size of a node changes the shape of the tree.
Why every leaf sits at the same depth, how B-trees relate to 2-3-4 and red-black trees, and why databases use them.
A complete implementation of search, insert and delete, explained part by part.
Check what you have learned before moving on.
// build your own
Download VisiGrab and step through every insert and delete yourself.
B-Tree is one of 30+ topics. See them all