B-Trees for Database Indexing: Structure, Properties, and Operations

B-Trees for Database Indexing: Structure, Properties, and Operations

Verified Sources
Sep 12, 2026

A keyword B-tree is a self-balancing search tree designed to keep height small by allowing each node to hold multiple keys and many child pointers. This is especially effective for database indexing because databases often measure cost in expensive storage accesses (e.g., disk/page reads), not in raw in-memory comparisons.2

A helpful mental model is: a B-tree generalizes a BST by replacing each single-key node with a “page-sized” node containing many sorted keys. The keys in a node act as separators that route search to the correct child subtree.

Key terms:

  • Order
  • Minimum degree t
  • Height h
  • Fanout
  • Separator key

Footnotes

  1. B-tree - Formal B-tree properties (Knuth order), sorted separator keys, and leaf-level uniformity. 2

  2. Locality, B-trees, and splay trees - Cache/page locality motivation for B-trees and why disk-locality matters.

B-Tree insertion and search intuition

Structure of a B-tree node

A typical B-tree node stores:

  • Several keyword keys in ascending order.
  • Pointers to children (subtrees). For a node with kk keys, there are k+1k+1 child pointers (for internal nodes). This is the “keys separate ranges” invariant.

Internal-node routing rule (range separation):

  • All keys in the leftmost child subtree are less than the first key.
  • Between key kik_i and ki+1k_{i+1}, the corresponding middle child subtree contains keys in that interval.
  • All keys in the rightmost child subtree are greater than the last key.

A B-tree is also height-balanced: all leaves appear at the same level.

Mermaid view of sorted keys separating ranges:

Footnotes

  1. B-tree - Formal B-tree properties (Knuth order), sorted separator keys, and leaf-level uniformity. 2

  2. B-trees and database indexes (Order + range routing intuition) - Explanation of node ordering and child routing relative to separator keys.

Order (and equivalent “minimum degree” form)

Different textbooks define “order” slightly differently. A common definition (Knuth) uses:

  • A B-tree of order mm has at most mm children per node.
  • A non-root/non-leaf node has at least m/2\lceil m/2\rceil children.

Wikipedia also highlights terminology ambiguity and explains that “order” can be defined as max children (Knuth) to avoid uncertainty.

In the “minimum degree” parameterization (often used in CLRS-style treatments):

  • Each node other than the root has at least t1t-1 keys.
  • Each node has at most 2t12t-1 keys.
  • Root has special-case bounds.

Key terms:

  • Root special case
  • Underflow
  • Overflow
  • Split

Footnotes

  1. B-tree - Formal B-tree properties (Knuth order), sorted separator keys, and leaf-level uniformity. 2

  2. 18.1 Definition of B-trees - CLRS Solutions - Minimum degree tt parameter relationships to keys per node.

Core B-tree properties (what makes it “correct” and balanced)

A B-tree maintains these invariants:

  1. Sorted keys within each node and range separation between children.
  2. Leaf level uniformity: all leaves are on the same depth.
  3. Capacity bounds based on the order/min-degree parameter:
    • at most mm children (Knuth order definition), with corresponding max keys per node.
    • non-root nodes have a minimum number of children/keys.
  4. The root is either a leaf or has at least two children.
  5. For any internal node: if it has kk children then it has k1k-1 keys (separators).

A compact invariant statement:

  • If a node has children C1,C2,,CkC_1, C_2, \dots, C_{k} and keys x1,,xk1x_1, \dots, x_{k-1}, then:
    • every key in C1C_1 is <x1< x_1
    • every key in CiC_i (for 1<i<k1<i<k) is in (xi1,xi)(x_{i-1}, x_i)
    • every key in CkC_k is >xk1> x_{k-1}.

Footnotes

  1. B-trees and database indexes (Order + range routing intuition) - Explanation of node ordering and child routing relative to separator keys. 2

  2. B-tree - Formal B-tree properties (Knuth order), sorted separator keys, and leaf-level uniformity. 2 3 4 5

Why B-trees are used for database indexing

The storage-access argument (reducing I/O)

Databases often store index nodes on disk pages. Each page access (cache miss) is far more expensive than comparing a handful of keys in memory. B-trees reduce the number of page accesses because they have high fanout and therefore small height.2

Wikipedia explicitly notes that multiway nodes reduce height and help because for disk-backed storage, fewer reads are needed to navigate to the right block.

Key terms:

  • Index
  • Page (block)
  • Blocking factor

How a B-tree index lookup works conceptually

An index entry is a keyword (and sometimes a pointer/record reference). A search:

  1. Starts at the root page.
  2. Uses the keys in the current node to decide which child pointer/page to follow.
  3. Repeats until reaching a leaf (or the node that may contain the key).

Time is often discussed as O(logn)O(\log n) comparisons and, more importantly, O(logbn)O(\log_{b} n) page reads where bb is an effective branching factor/blocking factor.

Footnotes

  1. Locality, B-trees, and splay trees - Cache/page locality motivation for B-trees and why disk-locality matters.

  2. B-tree - Notes on shorter height, reduced disk reads for auxiliary indexing, and why B-trees are used for storage. 2 3

  3. B-trees and database indexes (Order + range routing intuition) - Explanation of node ordering and child routing relative to separator keys.

B-tree insertion and search: a concrete example

We’ll use a small B-tree of order m=4m=4 (so each node can have at most 44 children, i.e., at most 33 keys, by the non-leaf relationship “kk keys ↔ k+1k+1 children”). This matches the “keys = children − 1” view and lets us demonstrate a split clearly.

In this simplified setting:

  • Max keys per node = m1=3m-1 = 3.
  • When a node would exceed 3 keys, we keyword split it and push a middle key upward (standard B-tree behavior). (The mechanics align with the usual B-tree insertion approach described in textbook summaries of splits/propagation.)

Insert sequence (keys)

Insert in order: 12, 7, 20, 4, 9, 3, 612,\ 7,\ 20,\ 4,\ 9,\ 3,\ 6

We’ll track the tree’s shape at key steps.

Footnotes

  1. B-tree - Formal B-tree properties (Knuth order), sorted separator keys, and leaf-level uniformity.

  2. B-Tree Time Complexity Overview | PDF - High-level insertion behavior (split propagation) and typical complexity claims for B-tree operations.

B-tree insertion (order $m=4$) with splits

  1. 1
    Step 1

    Root is a leaf with no keys.

  2. 2
    Step 2

    Root becomes [12].

  3. 3
    Step 3

    Root becomes [7, 12] (kept sorted).

  4. 4
    Step 4

    Root becomes [7, 12, 20] (still within max 3 keys).

  5. 5
    Step 5

    Would make 4 keys [4, 7, 12, 20] → overflow. Split the root: pick a middle key (here 7) to promote. After split:

    • New root: [7]
    • Left child: [4]
    • Right child: [12, 20]
  6. 6
    Step 6

    Compare with root [7]: 9 goes right. Right child [12, 20] → insert in sorted order → [9, 12, 20].

  7. 7
    Step 7

    Compare with root [7]: 3 goes left. Left child [4] → becomes [3, 4].

  8. 8
    Step 8

    Compare with root [7]: 6 goes left. Left child [3, 4] → becomes [3, 4, 6]. No overflow.

  9. 9
    Step 9

    Root [7]; left leaf [3, 4, 6]; right leaf [9, 12, 20].

Tree visualization after all insertions

Key terms:

  • Leaf node
  • Internal node
  • Promoted key

Note: Many databases use B+ trees for indexes, where records are stored at leaves and internal nodes store routing keys only; the routing logic is still B-tree-like. (This section focuses on B-tree mechanics and comparisons.)

Footnotes

  1. B-tree - Formal B-tree properties (Knuth order), sorted separator keys, and leaf-level uniformity.

B-tree search operation in the same example

  1. 1
    Step 1

    Start at root [7]. Since 6<76 < 7, follow the left child pointer.

  2. 2
    Step 2

    Left leaf keys are [3, 4, 6]. The key 6 is present → return found.

  3. 3
    Step 3

    At root [7], since 15>715 > 7, follow right child.

  4. 4
    Step 4

    Right leaf is [9, 12, 20]. 15 is not in the list → return not found (or nearest position depending on index API).

B-tree vs Binary Search Tree (BST) for database performance

Structural difference affecting height

  • A keyword BST has at most 2 children per node → tends to have larger height (worst case linear, and even with balancing, fewer keys per node).
  • A B-tree stores many keys per node and has higher fanout → smaller height and fewer storage-page reads.2

Wikipedia states that because B-trees have more than two children per node, a B-tree index will usually have shorter height than a BST, reducing reads during lookup.

Locality and “node-per-page” viewpoint

For database indexes on disk, performance is dominated by I/O latency. B-trees can pack multiple keys into one page, improving locality: you learn much more about the search interval from each page read than in a BST-like layout where a “node” contains only a couple of pointers/keys.2

Cornell’s lecture notes summarize the locality intuition: binary trees may waste cache lines because each node holds too few elements; B-trees store as many elements as fit in a cache line/page, improving practical performance.

Complexity comparison in practice

Both BST and B-tree are “O(logn)O(\log n)” in ideal balanced cases, but:

  • BST page reads depend on tree height with fanout 2, so height is larger.
  • B-tree page reads depend on height with fanout b2b \gg 2, so fewer levels are traversed.2

A simple comparative heuristic:

  • For B-tree: about logb(n)\log_{b}(n) page accesses (one per level).
  • For BST: about log2(n)\log_{2}(n) levels (one per node) when disk/page granularity aligns poorly with cache lines.

[CalloutBlock] type: "tip" title: "Rule of thumb for index choice" content: "When search cost is dominated by page reads, prefer high-fanout trees (B/B+ trees) over pointer-heavy binary structures."

Footnotes

  1. Locality, B-trees, and splay trees - Cache/page locality motivation for B-trees and why disk-locality matters. 2

  2. B-tree - Notes on shorter height, reduced disk reads for auxiliary indexing, and why B-trees are used for storage. 2 3 4 5

  3. Locality, B-trees, and splay trees - Binary trees vs B-trees locality and constant-factor performance benefits. 2

Qualitative page-read impact (fanout / height effect)

Illustrative comparison: higher fanout reduces height and therefore storage/page reads.

Common edge cases and clarifications

Knowledge Check

Question 1 of 4
Q1Single choice

Which property ensures that B-tree searches don’t become skewed over time?