Skip to content
CoreSource checked

B-Tree

Stores sorted keys in wide balanced nodes for efficient disk or page access.

Quick Facts

Compact metadata for implementation and discovery.

Category
Data Structures
Difficulty
Advanced
Family
Balanced Search Tree
Tier
Core
Status
Verified
Review
Source Checked
Sources
2
Formulas
0
Relationships
2
Code example
Not ready
Visualization
Ready

Overview

A B-Tree keeps many keys per node and maintains all leaves at the same depth, reducing expensive storage reads.

Core Concepts

stored invarianttree balancingsorted search spacebalanced search treeinvariant maintenancequery operationupdate operation

Quick facts

Complexity

Runtime and memory behavior at a glance.

Best

O(log n)

Average

O(log n)

Worst

O(log n)

Space

O(n)

When to Use

  • Use B-Tree when you need to keep keys sorted inside wide nodes.
  • Use it when the input assumptions match the method's model.
  • Use it when the operation mix matches the structure's strengths.
  • Use it when the method's tradeoffs fit the workload.

When Not to Use

  • Avoid it when the input cannot be kept sorted cheaply.
  • Avoid it when simpler arrays or maps meet the performance and clarity needs.
  • Avoid it when the required output is not balanced search tree.

What It Does

  • Keeps keys sorted inside wide nodes
  • Splits and merges nodes to stay balanced

Use Cases

  • Database indexes
  • File systems
  • On-disk sorted maps

Inputs

  • Keys and values
  • Node capacity

Outputs

  • Balanced search tree

Pseudocode

  1. 1.Search within the current node's sorted keys
  2. 2.Follow the child range that can contain the key
  3. 3.Insert into a leaf
  4. 4.Split full nodes on the way back up
  5. 5.Keep all leaves at the same depth

Canonical Example

Input
operations = insert 5, insert 2, query 5
Output
query result = found
Explanation
B-Tree: The structure stores data so later operations can answer efficiently.

Implementation Notes

  • Keep the input contract explicit: Keys and values; Node capacity.
  • Write operation-level tests that check both returned values and internal invariants.
  • Test updates and queries as sequences, not only one operation at a time.

Common Pitfalls

  • B-Trees are designed around block or page access rather than single pointer hops.
  • Forgetting to maintain invariants after insert, delete, merge, or update operations.
  • Handle empty, invalid, and boundary inputs before trusting balanced search tree.
  • Incorrect updates can leave stale pointers, counts, balance metadata, or cached summaries.

Interactive Mechanism Walkthrough

Graph Frontier & Edge Relaxation

Show multi-key nodes splitting while tree height stays small.

B-Tree mechanism walkthrough step 1 of 5suvwxt
Step 1 of 5: Search within the current node's sorted keys
wide-balanced-tree

Notes

B-Tree operates in O(log n) time and O(n) space within the Balanced Search Tree family. Often compared with Red-Black Tree and Binary Search Tree.

Sources (2)

What each reference supports for this entry.

B-Trees

Paper

Original B-tree paper supporting balanced multiway search trees for external storage.

Open source

Introduction to Algorithms (CLRS)

Book

Algorithms textbook support for B-Tree's invariant, correctness argument, and asymptotic analysis.

Open source

Explore Next

Good next jumps from the relationship graph, ranked for useful discovery.

Alternative toRed-Black Tree

B-trees optimize block-oriented ordered search, while red-black trees optimize pointer-based memory search.

Tags

Data StructureBalanced TreeSorted Data