B-Trees
Wide, short trees for disks and databasesA B-tree stores several sorted keys in every node, so it is short and wide. Databases and file systems use it because reading one node is one disk read. Inserting into a full node splits it, and deleting from a small node borrows from or merges with a sibling.
- Order mat most m − 1 keys per node
- Full nodesplit — the middle key moves up
- Balancedall leaves on the same level
- Current
- Compared
- Too many / few
- New / moved
Ready
Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode
Every operation, in plain steps
These are exactly the steps the visualizer animates.
What does a B-tree node store?
A B-tree of order m keeps up to m − 1 sorted keys in every node, and an inner node with k keys has k + 1 children. Child i holds the keys that lie between key i − 1 and key i. Every node except the root has at least ⌈m/2⌉ − 1 keys, and all leaves are on the same level — so the tree is always balanced.
#define ORDER 4 // m: at most 3 keys, 4 children
struct Node {
int n; // how many keys are used
int keys[ORDER - 1]; // sorted keys
struct Node *child[ORDER]; // child[i]: keys between keys[i-1] and keys[i]
int leaf; // 1 = no children
};
Search
- Scan the keys of the node from the left.
- Stop at the first key ≥ the key you want.
- Equal? Found. Otherwise go down into the child before it.
- One node per level — very few nodes in total.
Insert
- Walk down to the leaf where the key belongs.
- Put it into the leaf in sorted order.
- If the leaf now has m keys, it must split.
- New keys always go into leaves.
Split
- Take the middle key of the full node.
- Keys left of it stay; keys right of it form a new node.
- The middle key moves up into the parent.
- A full root splits into a new root: the tree grows at the TOP.
Build tree
- Start with an empty tree.
- Insert the typed keys one by one, in that order.
- Every full node splits; its middle key moves up.
- The tree grows at the top, whatever the key order.
Delete
- Find the key.
- In an inner node: replace it by its predecessor (largest key on the left).
- Remove the key from the leaf.
- If the node now has too few keys, fix it.
Borrow
- A sibling has more than the minimum?
- The separator in the parent moves down into the small node…
- …and the sibling's nearest key moves up to replace it.
- Like a rotation — sorted order is kept.
Merge
- Neither sibling can spare a key?
- Join the node, its sibling and their separator into one node.
- The parent loses a key — it may need fixing too.
- An empty root disappears: the tree shrinks at the top.
Pseudocode
Language-independent steps. The visualizer shows the same logic in C, Python and Java.
How fast is each operation?
n = number of keys, m = order. The height is about log_m n, so a B-tree of order 100 with a million keys has only 3–4 levels. Inside a node the keys are scanned (or binary searched), which costs O(m). Building a tree of n keys is n inserts, so O(n log n).
| Operation | Best | Average | Worst | Speed |
|---|---|---|---|---|
| Search | O(1) |
O(log n) |
O(log n) |
Grows with n |
| Insert (with splits) | O(log n) |
O(log n) |
O(log n) |
Grows with n |
| Delete (borrow / merge) | O(log n) |
O(log n) |
O(log n) |
Grows with n |
| Build tree (n keys) | O(n log n) |
O(n log n) |
O(n log n) |
Grows with n |
| Disk reads per search | 1 |
height |
height |
Instant |
Keys per node for different orders
| Order m | Max keys (m − 1) | Min keys (⌈m/2⌉ − 1) | Children | Also called |
|---|---|---|---|---|
| 3 | 2 | 1 | 2 or 3 | 2-3 tree |
| 4 | 3 | 1 | 2 to 4 | 2-3-4 tree |
| 5 | 4 | 2 | 3 to 5 | — |
| 100 | 99 | 49 | 50 to 100 | Typical database index |
B-tree vs binary search tree
| BST / AVL tree | B-tree | |
|---|---|---|
| Keys per node | 1 | Up to m − 1 |
| Children per node | Up to 2 | Up to m |
| Height for 1,000,000 keys | About 20 (AVL) | About 3–4 (m = 100) |
| Stays balanced by | Rotations (AVL) | Splitting and merging nodes |
| Best for | Data in memory | Data on disk: databases, file systems |
Check your understanding
Answer each question — you will see the explanation straight away.
In a B-tree of order 4, what is the most keys one node can hold?
A node of order m holds at most m − 1 keys: 4 − 1 = 3 (and has up to 4 children).
Inserting into a full leaf causes a split. What happens to the middle key?
The left keys stay, the right keys form a new node, and the middle key becomes the separator between them in the parent.
Where does a B-tree grow when it gets taller?
When the root splits, its middle key forms a new root. That is why all leaves always stay on the same level.
A node has too few keys after a delete, and its left sibling has spare keys. What happens?
That is a borrow: a rotation through the parent that keeps all keys in sorted order.
Why do databases use B-trees instead of binary search trees?
Every level costs a slow disk read. A B-tree with hundreds of keys per node needs only a handful of reads.
In a B-tree of order 5, every node except the root must have at least how many keys?
⌈5/2⌉ − 1 = 3 − 1 = 2 keys. Fewer than that is an underflow, fixed by borrowing or merging.
Trees quiz
Test yourself: 5 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.