What is a B-Tree?
A B-Tree generalizes the Binary Search Tree by letting each node hold multiple sorted keys and have more than two children. Instead of "smaller left, larger right," a node with k keys has k+1 children, and each child's entire range of values falls between two adjacent keys in the parent (or before the first / after the last).
Why Use a B-Tree?
This wide branching factor is the entire point: B-Trees were designed for data that lives on disk, not in memory. Reading from disk is orders of magnitude slower than reading from RAM, and each disk read typically pulls in a whole block regardless of how much of it you actually need — so it makes sense to pack as many keys as possible into a single node/block and minimize the number of levels (and therefore disk reads) needed to find anything. This is exactly why B-Trees (and their variant, B+ Trees) are the standard on-disk index structure in databases like PostgreSQL and MySQL's InnoDB, and in filesystems like NTFS and ext4.
Key Properties
Every B-Tree has a minimum degree t that fixes its shape: every node (except the root) must hold at least t-1 keys and at most 2t-1 keys, giving it between t and 2t children. Crucially, every leaf sits at exactly the same depth — a B-Tree never becomes lopsided the way an unbalanced BST can, because it grows upward from the root instead of downward from the leaves.
How Does a Node Split Work?
Algorithm Steps (Insertion)
- If the tree is empty, create a new leaf node holding just the new key
- If the root itself is full (has 2t-1 keys), split it first — this is the only way the tree grows taller
- Walk down from the root looking for the leaf where the key belongs. At each internal node:
- Find which child's range the key falls into
- If that child is full, split it before descending into it
- Move into that child and repeat
- Once a non-full leaf is reached, insert the key into its sorted position
Time Complexity
- Time Complexity: Height is O(log_t n) — search, insert, and delete all cost O(log_t n).
- Disk I/O: Since each node is one block, height directly bounds the number of disk reads needed.
Time Complexity Analysis
Insertion needs O(1) extra space per split — no recursion stack proportional to the tree's key count, just a constant amount of bookkeeping per level the insertion touches, and the visualizer here uses t = 2 (so nodes can hold up to 3 keys) purely to keep the diagram small; real-world B-Trees typically use a t sized to match a disk block, often in the hundreds.