A B-tree is an m-way search tree with rules that keep it balanced: every node except the root is at least half full, and every leaf sits at the same depth. It is to an m-way search tree what an AVL tree is to a binary search tree. B-trees keep large sorted collections a few levels tall, which is why databases and file systems use them.
On the m-way search tree page, nodes held up to m − 1 keys and had up to m children. That cut the height, but nothing kept the tree balanced: sorted input still made a chain. A plain BST has the same problem, and the AVL tree fixed it. The B-tree fixes it for m-way trees.
A B-tree of order m is an m-way search tree in which:
Keys still guide the search as in any m-way tree: for a key k in a node, keys in the child to its left are smaller than k and keys in the child to its right are larger.
Terminology: the order m is the most children a node may have. The root is the top node, internal nodes have children, and leaves have none.
| Order m | Keys per node (not the root) | Children per internal node (not the root) |
|---|---|---|
| 3 | 1 to 2 | 2 to 3 |
| 4 | 1 to 3 | 2 to 4 |
| 5 | 2 to 4 | 3 to 5 |
| 100 | 49 to 99 | 50 to 100 |
Every internal node has either 2 or 3 children: at most m = 3, and at least ⌈3/2⌉ = 2. Likewise an order-4 B-tree is a 2-3-4 tree.
A tree with only a few keys has to put them somewhere. With 1 key in an order-5 tree, the root holds 1 key, below the minimum of 2 that other nodes need. And when the root splits, the new root starts out with a single key.
The chain. In the plain m-way tree, a new key could start a new node one level below a full node, so one branch could grow far longer than the others. In a B-tree new nodes never appear below a leaf, so no branch can be longer than any other.
Search is exactly the m-way search, and it is close to a binary search with more than two ways at each step:
BTreeNode* BTree::search(BTreeNode* node, int key) {
if (node == nullptr)
return nullptr;
int i = 0;
while (i < node->keys.size() && key > node->keys[i])
i++;
if (i < node->keys.size() && key == node->keys[i])
return node;
if (node->isLeaf)
return nullptr;
return search(node->children[i], key);
}
Child 0 holds keys smaller than keys[0], child 1 holds keys between keys[0] and keys[1], and so on. If i keys are smaller than the target, it falls just after keys[i − 1] and before keys[i], which is child i's gap.
A new key always goes into a leaf, in sorted position. Search for it from the root to find the right leaf. If the leaf still has at most m − 1 keys, you are done.
If the leaf now has m keys, it has overflowed. Split it:
The parent has gained a key, so it may overflow too. Repeat the split on the way up. If the root overflows, its middle key becomes a new root above it. That is the only way a B-tree gets taller, and it adds a level above every leaf at once, so all leaves stay at the same depth.
The new node would be one level deeper than every other leaf, breaking the equal-depth rule. It would also hold only one key, likely below the minimum. Splitting and pushing the middle key up keeps both rules: the two halves stay on the leaf level, and the tree only gets taller when the root splits.
Yes. An overflowed node has m keys; one moves up and the other m − 1 are shared between the two halves, so each gets at least ⌊(m − 1)/2⌋ keys, which equals ⌈m/2⌉ − 1, the minimum. For order 4 that is 1 key on the left and 2 on the right.
70 goes into the leaf [50 60], making [50 60 70]. That is 3 keys, the most allowed in order 4, so there is no split. The next key to land in that leaf will cause one.
Each operation steps through the C++ on the right. Inserts go down to a leaf, then come back up checking each child for overflow and splitting where needed.
main() (order 3: 10 20 5 6 12 30 7 17). Before each insert, predict whether it causes a split.10, 20, 6, 12 and 17 fit without a split. 5 overflows the root [5 10 20], so 10 moves up to a new root. 30 overflows [12 20 30], and 20 moves up. 7 overflows [5 6 7], and 6 moves up, which overflows the root [6 10 20], so 10 moves up again to a new root: one insert, two splits, and the tree is now three levels tall. The final traversal is 5 6 7 10 12 17 20 30.
The B-tree has height 4 here; the plain 3-way tree is a chain of height 10. Sorted input cannot hurt a B-tree, because every split keeps the leaves level and the tree only grows at the root.
It is inorder traversal generalized to m ways. Everything in child i lies between keys[i − 1] and keys[i], so printing each child's keys before the key to its right produces increasing order.
Deletion is more involved, because removing a key can leave a node with fewer than ⌈m/2⌉ − 1 keys: an underflow.
Also at the root: when a merge takes the root's last key, the merged child becomes the new root. Every leaf moves up one level at once, so leaves stay at the same depth, the mirror image of a root split.
Every internal node except the root has at least ⌈m/2⌉ children, so the number of nodes at least multiplies by ⌈m/2⌉ at each level. That makes the height at most about log⌈m/2⌉ n, and at best about logm n when nodes are full. For large m the tree is extremely flat:
| 1,000,000 keys | Fewest levels | Most levels |
|---|---|---|
| BST (balanced at best) | 20 | 1,000,000 |
| B-tree, order 3 | 13 | 19 |
| B-tree, order 100 | 3 | 4 |
| B-tree, order 1000 | 2 | 3 |
On disk, each node is one block, so a search in an order-1000 B-tree of a million keys reads at most 3 blocks. Inside each node the work is O(m), or O(log m) with binary search, which is cheap compared with reading the block. With m treated as a constant, search, insert and delete are all O(log n), and a full traversal is O(n).
In memory, comparisons are what cost time, and a big node means many comparisons per level, so small nodes (or a balanced BST) do fine. On disk, reading a block costs far more than the comparisons inside it, so you make each node fill a whole block and get as few levels, and as few reads, as possible.
B+ trees keep every key (and its data) in the leaves. Internal nodes hold only copies of keys as signposts for the search, so more of them fit in each block, and the leaves are linked left to right in a list. Reading a range of keys in order means finding the first one and then walking along the leaves. Most databases and file systems use B+ trees for their indexes.
B* trees keep nodes at least two-thirds full instead of half full. When a node overflows, it first tries to pass keys to a sibling; only when both are full do the two split into three. Nodes end up fuller, so the tree uses less space and splits less often.