Data Structures · Visualization
B+ Tree: Insert & Split
B+ Tree
Inserts always land in a leaf. Time: O(log n) — one descent plus rare splits. Space: O(1) extra per insert.
Big-O: Point ops O(log n); range scan O(log n + k).
B+ Tree: Insert & Split
B+ TreeInserts always land in a leaf. When the leaf overflows, it splits in half and the middle key is copied (not moved) up to the parent as a new signpost. Splits can cascade upward, and if the root splits the tree grows one level — always at the top, so all leaves stay equally deep.
How Insert & Split Works
- Descend signposts to the correct leaf.
- Insert the key in sorted order inside the leaf.
- If the leaf overflows: split half/half, link the new leaf into the chain.
- Copy the middle key up to the parent; repeat if the parent overflows.
Insert & Split Worked Example
Input: Insert 25 into leaves [10] → [20, 30] → [40] (max 2 keys)
- Descend: 25 ≥ 20 → middle leaf [20, 30].
- Insert → [20, 25, 30]: overflow (3 > 2).
- Split → [20] and [25, 30]; copy 25 up; relink chain.
- Root signposts become [20, 25].
Output: 1 split, chain intact: [10] → [20] → [25, 30] → [40]
Press ▶ Run to watch it step by step.
def insert(root, key):
leaf = descend(root, key) # follow signposts
leaf.keys.insert_sorted(key)
if len(leaf.keys) > MAX:
left, right, mid = split(leaf)
parent_insert(mid, left, right) # copy up
# splits cascade; root split grows height
# example: insert 25 on [10]Behind the Scenes Execution
Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.
Insert & Split Time & Space Complexity
When to Use Insert & Split
- Bulk-loading sorted dumps into fresh database indexes
- Write-heavy key-value engines (WiredTiger, InnoDB)
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Ready for the next step?
See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.