B-Tree (Multi-way Balanced)

Dynamic SVG non-overlapping coordinate layout with interactive traversal and inspection

204010305060
Step 1 of 1Action: insert

Initialized B-Tree (degree t = 2).

Speed: 1x

Overview & Concept

A B-Tree is a self-balancing search tree optimized for systems that read and write large blocks of memory, such as disk storage engines and database systems. Unlike binary trees, B-Tree nodes have multiple keys and more than two children (degree M). By maximizing branching factor, B-Trees drastically minimize tree height and disk I/O operations.

How It Works

High Branching Factor: Each internal node contains up to M-1 keys and M child pointers.

All Leaves at Equal Depth: Perfect balance is maintained by growing the tree upwards from root splits.

Sorted Keys per Node: Keys within each node are sorted and act as separation values dividing subtrees.

Node Splitting & Merging: When a node reaches capacity, it splits at the median key, pushing it to parent.

Step-by-Step Execution

1. Root TraversalSearch keys within current node using binary search to find matching child branch.
2. Branch DescentDescend to child pointer between appropriate key boundaries.
3. Leaf InsertionInsert key into appropriate leaf node in sorted order.
4. Split on OverflowIf leaf exceeds max keys, split into two halves and promote middle key to parent.

Asymptotic Complexity Analysis

MetricBest CaseAverage CaseWorst CaseSpace Complexity
Complexity BoundO(log n) boundedO(log n) search/insertO(log n) disk readsO(n)
ExplanationOptimal input arrangement minimizing operations.Expected time for uniformly distributed randomized inputs.Adversarial degenerate conditions requiring full traversal.Auxiliary memory allocated beyond original input size.

Pseudocode & Invariants

Algorithm Logic
// B-Tree Node Insertion & Splitting
operation insertNonFull(node, key):
  i = node.numKeys - 1
  if node.isLeaf:
    while i >= 0 and key < node.keys[i]:
      node.keys[i + 1] = node.keys[i]; i = i - 1
    node.keys[i + 1] = key; node.numKeys++
  else:
    while i >= 0 and key < node.keys[i]: i = i - 1
    i = i + 1
    if node.children[i].isFull: splitChild(node, i)
    insertNonFull(node.children[i], key)
Algorithmic Invariants
  • Height Invariant: All leaf nodes appear at the exact same depth.
  • Key Capacity Invariant: Every node (except root) contains at least ⌈M/2⌉ - 1 keys and at most M - 1 keys.
Correctness Guarantee: Invariants hold true prior to loop entry, are preserved during every iteration, and guarantee correct post-condition upon termination.

Advantages & Disadvantages

Key Advantages

  • •Extremely shallow tree height, minimizing disk block reads.
  • •Superb disk block cache efficiency and sequential I/O throughput.
  • •Guaranteed O(log n) worst-case time for search, insertion, and deletion.

Trade-offs & Limitations

  • •Complex rebalancing, node splitting, and merging logic.
  • •Nodes may be partially empty (wasting up to 50% internal space).

Real-World Applications

Relational Database Indexes

PostgreSQL, MySQL InnoDB, and Oracle B-Tree and B+Tree table indexes.

Operating System File Systems

NTFS, ext4, and APFS directory record trees.

Decision Guide: When to Use

Ideal Scenarios

  • ✓When managing data stored on block storage devices (HDDs, SSDs, cloud disks).
  • ✓When building relational database indexes and file systems.

Consider Alternatives When

  • ✕When managing purely in-memory data with small footprints (use AVL or Red-Black).

Compare trade-offs and complexity across complementary tools in AlgoViz.