Tree

B-tree Insertion

Insert keys while preserving sorted order, balanced height, and controlled node splitting.

B-tree insertion becomes difficult when learners first encounter overflow, median promotion, and split propagation. The goal of this page is to make those structural changes clear, step by step, before students think about implementation details.

The key teaching flow is: insert in order → detect overflow → split the node → promote the median → continue upward if needed.

Category
Tree
Core Process
Insert • Split • Promote
Main Event
Overflow
Structural Goal
Balanced Height

Quick Facts

Ordered Keys

Keys inside each node remain sorted from left to right.

Overflow Trigger

When a node receives too many keys, it must split.

Median Promotion

The middle key moves upward so order is preserved across the tree.

Balanced Growth

Insertion preserves balanced height, even when splits propagate upward.

Key Idea

B-tree insertion is not just “put the key somewhere.” It is a structural process that must preserve both sorted order and balance.

The most important mental model is: insert locally, but reason globally. A small change in one node can cause structural changes higher in the tree.

Visualization

This visualization shows the full insertion story: ordered insertion inside a node, overflow becoming visible, the median key moving upward, and the way splits can continue toward the root.

Watch actively: pause before a split, predict which key will be promoted, and decide whether the structural change will stop or continue upward.

How It Works

1
Find the Correct Position

Follow the search path until the correct leaf or target node is reached.

2
Insert in Sorted Order

Place the new key into the node while keeping the keys ordered.

3
Check for Overflow

If the node now holds too many keys, a split is required.

4
Split and Promote

Promote the median key upward and redistribute the remaining keys into two nodes.

Why Overflow Matters

Visible Trigger

Overflow is the signal that the node can no longer store another key without restructuring.

Median Choice

The median is promoted because it preserves correct ordering between the two resulting nodes.

Local to Global Change

A split starts locally, but the promoted key may force changes in the parent as well.

Root Growth

If promotion reaches a full root, the tree gains a new level while staying balanced.

The Structural Pattern

Every split follows the same reusable logic:

Detect overflow
Highlight the median
Promote the median upward
Redistribute remaining keys
Reconnect children if needed
Check the parent for overflow

Seeing this as a repeated pattern makes B-tree insertion much easier to understand than memorizing isolated cases.

Why It Matters

Data Structure Reasoning

B-tree insertion teaches how local ordering rules can preserve large-scale structural balance.

Search Performance

Balanced growth helps B-trees keep search, insertion, and update operations efficient.

Database Relevance

B-trees are widely used in indexing systems, making insertion logic practically important.

Better Than Memorization

When learners understand overflow and promotion, they can predict tree changes instead of guessing.

Pseudocode

A common high-level version of B-tree insertion looks like this:

B-TREE-INSERT(T, k):
    r = T.root

    if r is full:
        create new root s
        make old root a child of s
        SPLIT-CHILD(s, 0)
        INSERT-NONFULL(s, k)
    else:
        INSERT-NONFULL(r, k)


INSERT-NONFULL(x, k):
    if x is a leaf:
        insert k into x in sorted order
    else:
        choose the correct child of x
        if that child is full:
            SPLIT-CHILD(x, child_index)
            decide again which child should receive k
        INSERT-NONFULL(correct_child, k)

The important idea is that insertion tries to move downward into a node that can still accept a key, and splitting happens before the structure becomes invalid.

Common Confusions

“Any key can be promoted”

No. The median is promoted because it preserves the correct partition between left and right keys.

“Splitting is the same as inserting”

Insertion adds a key. Splitting is the structural repair that happens only after overflow.

“The split always stops immediately”

Not always. Promotion can cause the parent to overflow, which may trigger another split.

“Root overflow breaks the tree”

Root overflow does not break the tree. It creates a new root and increases the height by one.

Outcome

After this lesson, learners should be able to insert a key into a B-tree in sorted order, recognize overflow, explain why the median is promoted, and describe how split propagation preserves balance.