B+ Tree

Multi-way search tree, database indexing, range queries

Introduction

A B+ Tree is a self-balancing multi-way search tree designed specifically for efficient storage and retrieval of large amounts of data from disk.

It is an extension of the B-Tree and is one of the most important data structures used in:

  • Database Management Systems (DBMS)
  • File Systems
  • Search Engines
  • Storage Engines
  • Distributed Databases

Popular databases using B+ Trees:

  • MySQL (InnoDB)
  • PostgreSQL
  • SQLite
  • Oracle Database
  • Microsoft SQL Server

Why Do We Need B+ Trees?

Consider storing millions of records.

If we use:

Array

Search = O(n)

Too slow.


Binary Search Tree

Search = O(log n)

Good in memory but inefficient for disk-based storage because each node stores only one key.

This leads to many disk accesses.


B+ Tree

Stores many keys per node.

Example:

[10 | 20 | 30 | 40]

Instead of:

10
 \
 20
   \
    30
      \
       40

Result:

Fewer levels
Fewer disk reads
Faster performance

What is a B+ Tree?

A B+ Tree is a balanced m-way search tree where:

  • Internal nodes store only keys.
  • Leaf nodes store actual data.
  • All leaf nodes are linked together.
  • All leaves exist at the same level.

Basic Structure

Example:

                [20 | 40]

             /      |      \

        [5|10]   [25|30]   [50|60]

           |         |         |

      -------------------------------
      |      |      |      |      |
   [1,3] [5,8] [25,28] [40,45] [50,70]

      <---- Linked Leaves ---->

Terminology


Root Node

Topmost node.

[20 | 40]

Internal Node

Contains:

  • Keys
  • Child Pointers

Does not store actual records.

Example:

[20 | 40]

Leaf Node

Stores:

  • Keys
  • Record Pointers (or actual records)

Example:

[25,28]

Order (m)

Maximum number of children per node.

Example:

Order 4 Tree:

Maximum Children = 4
Maximum Keys = 3

Formula:

Max Keys = m - 1

Properties of B+ Tree

A B+ Tree of order m follows:


Property 1

Every node can have at most:

m children

Property 2

Every internal node contains:

m - 1 keys

at maximum.


Property 3

Root node must have at least:

2 children

unless it is the only node.


Property 4

All leaves are at the same level.

This guarantees balance.


Property 5

Leaf nodes are linked together.

Leaf1 <-> Leaf2 <-> Leaf3

This enables efficient range queries.


Property 6

Internal nodes contain only keys.

Actual records exist only in leaf nodes.


Structure of Internal Node

Example:

[20 | 40 | 60]

Children:

P0 P1 P2 P3

Rules:

P0 < 20

20 ≤ P1 < 40

40 ≤ P2 < 60

P3 ≥ 60

Structure of Leaf Node

Example:

[10 | 15 | 18]

Stores:

Key
Pointer to Record
Pointer to Next Leaf

Representation:

[10|15|18] ---> [20|25|30]

B+ Tree Example

Order = 4

Insert:

10
20
30
40
50
60
70

Result:

                 [30 | 50]

               /     |      \

          [10|20] [30|40] [50|60|70]

Leaf Links:

[10|20] <-> [30|40] <-> [50|60|70]

Searching in B+ Tree

Suppose we search:

40

Tree:

                 [30 | 50]

               /     |      \

          [10|20] [30|40] [50|60|70]

Steps:

Step 1

Start at root.

40 > 30
40 < 50

Move to middle child.


Step 2

Search leaf.

[30|40]

Found.


Search Complexity

Height:

O(logₘ n)

Search Complexity:

O(log n)

Insertion in B+ Tree

Insertion occurs in leaf nodes.


Step 1

Locate appropriate leaf.


Step 2

Insert key.


Step 3

If leaf overflows:

Split Node

Step 4

Promote key to parent.


Example

Order = 4

Current:

[10 | 20 | 30]

Insert:

40

Overflow:

[10 | 20 | 30 | 40]

Split:

[10 | 20]

[30 | 40]

Promote:

30

Parent receives:

[30]

Node Splitting

Node splitting maintains balance.

Before:

[10|20|30|40]

After:

[10|20]

[30|40]

Parent:

[30]

Deletion in B+ Tree

Deletion occurs only in leaf nodes.


Step 1

Find key.


Step 2

Delete key.


Step 3

Check minimum occupancy.


Step 4

If underflow occurs:

Choose one:

Redistribution

Borrow key from sibling.

Merge

Merge with sibling.


Redistribution Example

Before:

[10]

[20|30|40]

Borrow:

[10|20]

[30|40]

Balanced.


Merge Example

Before:

[10]

[20]

Merge:

[10|20]

Parent updated.


Why Are Leaf Nodes Linked?

One of the biggest advantages of B+ Trees.


Range Query Example

Find:

20 to 60

Leaves:

[10|15]
   ↓
[20|25]
   ↓
[30|40]
   ↓
[50|60]

Traversal becomes:

20
25
30
40
50
60

Very efficient.


B+ Tree Traversal

Unlike Binary Trees:

Preorder
Inorder
Postorder

are rarely used.

Most operations involve:

  • Search
  • Range Search
  • Sequential Scan

using linked leaves.


Height of B+ Tree

Height remains very small.

Example:

Suppose:

Fanout = 100

Then:

Level 1 = 100 nodes

Level 2 = 10,000 nodes

Level 3 = 1,000,000 nodes

Only 3 levels needed.

This is why databases prefer B+ Trees.


Time Complexity

OperationComplexity
SearchO(log n)
InsertO(log n)
DeleteO(log n)
Range QueryO(log n + k)

where:

k = number of records returned

Space Complexity

Storage:

O(n)

B+ Tree vs Binary Search Tree

FeatureBSTB+ Tree
BalancedNot AlwaysAlways
Disk FriendlyNoYes
SearchO(log n) AverageO(log n)
Range QueryPoorExcellent
Multi-Key NodeNoYes

B+ Tree vs B-Tree


B-Tree

Stores data in:

  • Internal Nodes
  • Leaf Nodes

Example:

[10, Record]

B+ Tree

Stores data only in:

Leaf Nodes

Internal nodes contain only keys.


Comparison

FeatureB-TreeB+ Tree
Data StorageInternal + LeafLeaf Only
Leaf LinkingNoYes
Range QueriesSlowerFaster
Sequential AccessPoorExcellent
DBMS UsageLess CommonMost Common

Advantages of B+ Tree


Efficient Searching

O(log n)

Disk Optimized

Reduces disk I/O.


Efficient Range Queries

Thanks to linked leaves.


Always Balanced

Height remains small.


Better Cache Utilization

Stores multiple keys per node.


Disadvantages of B+ Tree


More Complex

More complicated than BST.


Extra Pointers

Leaf linking requires additional pointers.


Splitting and Merging Overhead

Insertion and deletion are more complex.


Applications of B+ Tree


Database Indexing

Most common use.

Example:

CREATE INDEX idx_name
ON employee(name);

Implemented using B+ Trees.


File Systems

Examples:

  • NTFS
  • HFS+
  • XFS

Search Engines

Store large indexes efficiently.


Key-Value Stores

Examples:

  • RocksDB
  • Berkeley DB

Distributed Databases

Efficient lookup and range scans.


Interview Questions

Easy

  1. What is a B+ Tree?
  2. Why are leaves linked?
  3. What is the order of a B+ Tree?
  4. Difference between B-Tree and B+ Tree?

Medium

  1. Explain insertion.
  2. Explain deletion.
  3. What causes node splitting?
  4. What causes merging?

Advanced

  1. Why do databases prefer B+ Trees over BSTs?
  2. Why is B+ Tree disk optimized?
  3. Explain range query execution.
  4. Compare AVL Tree, B-Tree, and B+ Tree.

Summary

A B+ Tree is a self-balancing multi-way search tree optimized for disk-based storage systems.

Key Characteristics

  • Multi-way tree
  • Balanced structure
  • Internal nodes store only keys
  • Actual records stored in leaves
  • Leaves linked together
  • Excellent range query performance

Complexity

OperationComplexity
SearchO(log n)
InsertO(log n)
DeleteO(log n)
Range QueryO(log n + k)

Major Use Cases

  • Database Indexes
  • File Systems
  • Search Engines
  • Storage Engines
  • Distributed Databases

B+ Trees are among the most important data structures in database systems because they minimize disk I/O while providing fast search, insertion, deletion, and range-query performance.