Best on a larger screen — the animation and controls have more room to breathe.
Inputs

B+ Tree

Unlike a plain B-tree that stores records in every node, a B+ tree splits routing from storage. Internal pages hold only guide keys; leaf pages hold every record in sorted order at one shared depth, linked left-to-right. Keeping records out of internal pages lets each upper page pack more children for O(log_m n) point lookups, while linked leaves let range queries descend once and scan sideways in O(log_m n + k).

That separation creates two distinct split rules. Splitting a full leaf copies the right leaf's first key upward into the parent while keeping that key in the right leaf, so the leaf chain never loses a record. Splitting a full internal page pushes its middle guide upward and removes it from both split internal children: a page with c children holds c - 1 guides, so dividing c children across two pages leaves one fewer guide in total while the pushed middle key separates the two halves above them.

In a database, one ordered B+ tree index can therefore serve WHERE key = ?, BETWEEN, and a compatible ORDER BY scan.

Guides Route Above, Linked Leaves Hold Every Record

This lesson uses order (m) to mean the maximum number of children in an internal page. A settled order-m page holds at most m - 1 keys. At order 3, each page holds up to two keys; a third key overflows the page and forces a split.

An internal page with guides [15, 30] splits keys across three child intervals:

  • child 0 receives keys < 15;
  • child 1 receives keys 15 <= key < 30; and
  • child 2 receives keys >= 30.

Equality always routes right (>=). Copied leaf separators remain at the start of the right leaf, so going right on a match leads straight to the stored record. Reaching a matching guide inside an internal page never ends a search: internal keys are routing guides, while the leaf chain holds the complete sorted sequence of records. A key that appears in both layers is still one logical record, not two.

Leaf Splits Copy Up; Internal Splits Push Up

Insertion descends to one leaf, locates the sorted slot, and writes the record key. If the page stays within m - 1 keys, the insert finishes. When it exceeds m - 1, the split rule depends on the page kind.

For a leaf split, the overflowing keys divide at ceil(len / 2)—two keys on the left and one on the right at order 3. The new right leaf first inherits the old leaf's next pointer (right.next = leaf.next), and only then does the old leaf point to the new right leaf (leaf.next = right). Writing the pointers in that order prevents losing the old successor during a non-rightmost split. Finally, the right leaf's first key is copied up into the parent as a guide while remaining in the leaf layer for range scans.

For an internal split, the middle guide at floor(len / 2) is removed from the overflowing page and pushed upward. Keys and children to its left stay in the old internal page; keys and children to its right move into a new internal page. Because every internal page with c children holds c - 1 guides—at order 3, 4 children split into two pages of 2 children and 1 guide each—the two split halves need one fewer guide in total, and the pushed middle key moves up to separate them while belonging in neither child. The corresponding record key still lives untouched in its leaf page below. If no parent exists, the pushed guide becomes a new root, raising every leaf's depth by one together.

B+ Tree Copy, Push, and Scan: Step by Step

The default run uses order 3 and executes:

insert 10, insert 20, insert 5, insert 15, insert 25, insert 30, search 20, range 9 27

  1. Insert 10 and 20. The root is a single leaf page, [10, 20].
  2. Insert 5. The leaf overflows as [5, 10, 20] and splits 2 + 1 into [5, 10] and [20]. Separator 20 is copied up into a new root while staying in leaf [20].
  3. Insert 15. It lands in the left leaf, overflowing [5, 10, 15] into [5, 10] and [15]. Because this split is not at the right edge, new leaf [15] first inherits [5, 10]'s old successor [20], then [5, 10] links to [15]. Separator 15 is copied into the root.
  4. Insert 25. Guide 20 routes 25 >= 20 right, inserting into [20, 25] with no split.
  5. Insert 30. The right leaf overflows as [20, 25, 30] and splits into [20, 25] and [30]. Copying 30 into the root makes the root guides [15, 20, 30] (4 children), triggering an internal split. Middle guide 20 is pushed into a new root: left internal [15] now separates [5, 10] and [15], right internal [30] separates [20, 25] and [30], and 20 leaves both internal children while record 20 remains in leaf [20, 25].

The final structure is:

                 [20]
              /        \
            [15]       [30]
           /   \       /   \
      [5,10]  [15] [20,25] [30]
          └────→────→────→

The leaf chain is [5, 10] -> [15] -> [20, 25] -> [30]. Every record appears once in that chain, even though 15, 20, and 30 also serve as internal guides.

Point Search Stops in One Leaf; Range Scans Follow Links

search(20) hits guide 20 immediately at the root, yet does not stop there because root 20 is only a guide. Equality routes right (20 >= 20) to internal page [30], then 20 < 30 routes left to leaf [20, 25]. Binary search inside that leaf finds record 20. The lookup visits three pages—one per level—and stops.

range(9, 27) begins with one root-to-leaf descent to lower bound 9, landing at key 10 inside [5, 10]. It emits 10, follows next to emit 15, follows next across the root boundary to emit 20 and 25, then follows one more link and halts when it encounters 30 > 27. The query returns [10, 15, 20, 25] after three leaf-link hops without touching the root again.

Sorted leaves alone are not enough: the next pointers keep cross-page scans sequential instead of restarting from the root for every emitted key.

When to Choose a B+ Tree Over a Hash Index

Choose a B+ tree when queries need both exact-key lookup and ordered access (BETWEEN, prefix bounds, ORDER BY). A hash index can serve equality-only lookups well, but hash buckets have no sorted leaf chain for range scans or ordered output. Database engines differ in naming and on-disk page layout, so engine-specific storage formats stay outside this lesson's model.

B+ Tree Complexity and the +k Scan Cost

Let n be the number of records, m the order, and k the number of returned records.

OperationTime in the modelWhy
Point searchO(log_m n) page levelsone child is chosen at each internal page
Insert without a cascadeO(log_m n)descend once, then edit one leaf
Insert with splitsO(log_m n)at most one split propagates per level
Range queryO(log_m n + k)descend to the lower bound once, then scan linked leaves

High fanout m keeps tree height small: real page-sized internal nodes hold hundreds of guides, so millions of records fit in a few levels. A page visit is a logical lookup (potentially served from a buffer pool), not necessarily a physical disk read. This lesson uses orders 3 through 5 so every slot and pointer remains legible; those tiny orders are for teaching, not production tuning.

B+ Tree Edge Cases

  • Empty tree: the root starts as one empty leaf. Search misses immediately, and a range returns an empty list.
  • First split: one leaf root splits into two leaves under a new internal root, raising height from 0 to 1.
  • Duplicate key: this unique-key teaching model detects the match in the target leaf and makes no change. Production indexes can support duplicates via engine-specific row identifiers or payloads.
  • Missing point key: descent reaches one candidate leaf; absence in that leaf ends the search without checking siblings.
  • Empty range: when the lower-bound slot already exceeds hi, the scan returns [] without following any link.
  • Range beyond the maximum: the scan follows next pointers until the tail's null marker. The Wide range scenario (under Scenarios at the bottom of the Inputs panel) runs range(1, 99) to return all eight stored keys and expose the full + k cost.
  • Visualization limit: insert remains clickable at 12 records so the trace can state that only the canvas cap is full; a B+ tree itself has no 12-record limit.

Common Mistakes in B+ Trees

  • Pushing a leaf separator. Removing the separator from both leaves deletes a record from the leaf-only data layer.
  • Copying an internal separator. Leaving the pushed middle guide inside either split internal child breaks the child-count-to-guide-count invariant and corrupts routing intervals.
  • Routing equality left. Copied separators mark the first key of the right subtree, so key == guide must route right.
  • Stopping a search at a matching internal guide. Internal guides only route; the record itself lives in a leaf.
  • Overwriting leaf.next before saving it. Always wire right.next = leaf.next before setting leaf.next = right.
  • Restarting each range output at the root. Repeated tree descents preserve order but discard the O(log_m n + k) sequential scan.
  • Reading schematic pages as a disk format. These pages model fanout, balance, and leaf linkage, not PostgreSQL or MySQL page bytes.

A Note on Simplification

This lesson models a textbook, in-memory B+ tree with unique integer keys, fixed order, immediate half splits, and no deletion. It omits buffer pools, physical page I/O, fill factors, prefix compression, duplicate record lists, concurrency latches, write-ahead logging, MVCC, recovery, and engine-specific page layouts. Real database indexes also store row payloads or pointers and use far higher fanout. The model isolates the structural contract: internal guides route, leaves retain every record, split rules preserve balance, and leaf links make ordered scans sequential.