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+ Tree

Inserts 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

  1. Descend signposts to the correct leaf.
  2. Insert the key in sorted order inside the leaf.
  3. If the leaf overflows: split half/half, link the new leaf into the chain.
  4. 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)

  1. Descend: 25 ≥ 20 → middle leaf [20, 30].
  2. Insert → [20, 25, 30]: overflow (3 > 2).
  3. Split → [20] and [25, 30]; copy 25 up; relink chain.
  4. Root signposts become [20, 25].

Output: 1 split, chain intact: [10] → [20] → [25, 30] → [40]

Try your own input
B+ Tree: Introduction
Signposts inside, values in chained leaves — the database index layout.
root signposts: [30]
10
20
leaf 0
30
40
leaf 1
Active leaf path Found key
B+ Tree: Introduction
Press ▶ Run to watch it step by step.
Python · B+ Tree in Code
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]
Input: Insert 25 into leaves [10] → [20, 30] → [40] (max 2 keys)

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

TimeO(log n) — one descent plus rare splits
SpaceO(1) extra per insert

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

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.