logo
SEE ALGORITHMS

B+ Tree Visualization

A B+ Tree is a self-balancing search tree and a variant of the B-Tree. Like a B-Tree, each internal node can hold multiple keys and have more than two children. The key difference is that in a B+ Tree all data records are stored only in the leaf nodes, while internal nodes act purely as routing guides. Leaf nodes are also connected in a Linked List, enabling efficient range queries without traversing the tree.

When inserting a new key, it is placed into the appropriate leaf node. If the leaf overflows, it splits — the smallest key of the right half is copied (not moved) up to the parent as a separator, so the key remains in the leaf. This copying behaviour is what distinguishes a B+ Tree from a B-Tree. Internal nodes split using the traditional median-push approach. The splitting process can propagate upward and may create a new root, always keeping all leaves at the same depth.


Key Properties

  • Data Exclusively in Leaves: Internal nodes store only routing keys and child pointers; all data records reside strictly in the leaf level.
  • Sequential Leaf Chain: Leaf nodes are linked as a continuous singly or doubly linked list, supporting O(k) sequential scans.
  • Key Replication: Keys appear in internal nodes as routing separators and are duplicated in the leaf nodes where the actual records exist.
  • Shallow Height: Omitting data payloads from internal nodes allows more keys per block, yielding lower tree depth than standard B-Trees.

Key Operations

  • Point Search: Navigates internal routing keys down to the target leaf, ensuring identical search path lengths for all keys.
  • Range Query: Finds the start key in O(log n), then follows the leaf chain pointers to collect records in range.
  • Leaf Split (Copy Up): When a leaf overflows, the smallest key of the right half is copied up to the parent so the key remains in the leaf.

Visualizer

For simplicity, the order of this B+ Tree visualizer is fixed to 3.

AI Summary

(5 credits)

See what happens with your input

Get a short, step-by-step explanation of this visualization. Turn on AI Summary, then start a fresh run with your input.


How It Works

A search operation in a B+ Tree always traverses from the root down to a leaf node. At each internal node, the algorithm compares the search key against the routing keys to determine which child subtree to visit. Even if an exact match is found inside an internal node, traversal continues downward into the child branch because the actual data record pointer is stored only in the leaf.

To execute a range query (e.g. finding all values between 20 and 50), the B+ Tree first performs a standard search to locate the leaf containing the lower bound. From there, it simply follows the sequential next pointer of the leaf nodes to scan records directly in sorted order until exceeding the upper bound, completely avoiding tree traversal overhead.

When to Use

B+ Trees are the universal default storage engine index for production relational databases, including MySQL (InnoDB), PostgreSQL, SQLite, and Microsoft SQL Server, as well as modern file systems like NTFS and XFS. Because real-world database queries frequently involve range filters (between, >=, <=) and sorting (order by), the linked leaf structure provides dramatically higher query throughput than standard B-Trees or hash indexes.

Time & Space Complexity

Metric / Operation
Complexity
Description
Point SearchO(log n)Uniform height ensures all searches reach the leaf level in exactly the same logarithmic number of steps.
Range QueryO(log n + k)Locates the starting leaf in O(log n) time, then traverses k matching elements sequentially via the leaf linked list.
InsertionO(log n)Inserts into a leaf node; splitting copies the separator key up to internal routing nodes in O(log n) time.
DeletionO(log n)Removes the key from the leaf; rebalancing borrows from adjacent leaves or merges leaves and updates parent routing keys.
Space ComplexityO(n)Stores all n records in leaves plus routing keys in internal nodes. High fanout keeps overhead low.

Common Interview Questions

Why does a B+ Tree support range queries more efficiently than a B-Tree?

B+ Tree leaf nodes are linked in a sorted linked list. A range query only needs one O(log n) traversal to find the start key, then a linear scan along the leaf chain to collect all records in range — no backtracking up the tree. In a B-Tree, data scattered across internal nodes forces repeated upward/downward traversals.

What are the time complexities for search, insert, and range queries in a B+ Tree of order m with n keys?

All three basic operations — search, insert, and delete — run in O(log n) with a base of m (tree height ~ logm n). A range query retrieving k records costs O(log n + k): one top-down traversal to the first leaf plus a linear scan of k leaf entries.

Why do relational databases use B+ Trees rather than B-Trees for table indexes?

B+ Trees give databases two key advantages: 1) Higher fanout — internal nodes hold only keys (no data records), fitting more separators per disk page and reducing tree height. 2) Efficient range scans — the leaf linked list enables BETWEEN, ORDER BY, and full table scans without revisiting internal nodes, which is critical for OLAP and query optimizers.


💬  Discussion

to join the discussion


Curious to Learn More?

Hand-picked resources to deepen your understanding


Learn DSA on Udemy
Learn DSA on Udemy
As an Udemy Associate, I earn from qualifying purchases.

© 2025 See Algorithms. Code licensed under MIT, content under CC BY-NC 4.0