G27
Self-checkedTier 1 10 min readdbms.b-tree

B trees

B trees as balanced multiway search trees: definition by order (at most m children, at least ceil(m/2) for non-root nodes), the node layout in a disk block and how the order follows from the block size, search, insertion with node splitting, deletion with borrowing and merging, the minimum and maximum number of keys for a given height and the height bounds for n keys; every operation shown is a snapshot of a real tree and the extreme trees were built node by node.

Why it exists

Binary search trees have height about log⁡2n\log_2 n, too many disk reads for a big index. A B tree is a search tree whose nodes are as large as a disk block and hold many keys, so the tree is very flat: a few block reads find any key, and the tree stays balanced under insertions and deletions without reorganising the whole file. GATE asks for the order of a B tree for a given block size, the minimum and maximum number of keys or nodes for a given height, and the result of insertions and deletions.

Prerequisites

You need ordered indexes (ordered indexes) and binary search trees (binary search trees); a B tree is the multiway generalisation of a BST that stays balanced.

Precise definitions

A B tree of order mm (maximum number of children, m≥3m \ge 3) is a rooted tree such that:

  1. every node has at most mm children and at most m−1m - 1 keys;
  2. every node except the root and the leaves has at least ⌈m/2⌉\lceil m/2 \rceil children, so every non-root node has at least ⌈m/2⌉−1\lceil m/2 \rceil - 1 keys;
  3. the root has at least 22 children unless it is a leaf;
  4. a node with kk keys k1<⋯<kkk_1 < \cdots < k_k has k+1k + 1 children (if it is not a leaf), and all keys in the ii-th subtree lie between ki−1k_{i-1} and kik_i;
  5. all leaves are on the same level.

Each key of an inner node comes with a pointer to the record (or the record itself): in a B tree every key appears exactly once in the tree, in an inner node or in a leaf.

Order from the block size. A node with pp tree pointers holds p−1p - 1 keys and p−1p - 1 record pointers. With block size BB, tree pointer PP, key VV and record pointer PrP_r: the node must satisfy p⋅P+(p−1)(V+Pr)≤Bp \cdot P + (p - 1)(V + P_r) \le B, so p=⌊B+V+PrP+V+Pr⌋.p = \Big\lfloor \frac{B + V + P_r}{P + V + P_r} \Big\rfloor. Example: B=512B = 512, V=9V = 9, P=6P = 6, Pr=7P_r = 7: p=⌊528/22⌋=24p = \lfloor 528/22 \rfloor = 24.

How it works

Start at the root. In a node, find the first key ≥\ge the search key by comparing with the keys in order (or binary search inside the node). If it is equal, found. Otherwise follow the child pointer between the two keys around the search key. At a leaf with no match the key is absent. At most hh nodes (blocks) are read, where hh is the height; a search may stop earlier at an inner node.

Insertion

  1. Search for the key; if present, stop (keys are unique).
  2. The key goes into the leaf where the search ended (a B tree grows at the leaves).
  3. If the node now has mm keys (one more than allowed) it overflows: split it around its median key (position m//2m // 2, counting from 00); the median moves up into the parent; the left part stays, the right part forms a new node. The parent may overflow in turn, and so on.
  4. If the root splits, a new root with the single median key is created: the tree grows one level taller at the top, so all leaves stay at the same depth.

Insertions into a B tree of order 4: start — start

Real tree, not typed
(empty)

Outlined node = the node the step works on; shaded = created in this step; bold key = the key being searched or moved.

Step 0

An empty B tree of order 4: a node holds at most 3 keys and at least 1 (non-root). A key is inserted into a leaf; an overflowing node is split around its middle key, which moves up.

0 / 21
computed by script: the file and index sizes are computed from their formulas, the trees are built by real insertions and deletions. python3 scripts/gen-db4-traces.py

Inserting 10,20,30,40,50,60,70,15,2510, 20, 30, 40, 50, 60, 70, 15, 25 into an empty tree of order 44 (at most 33 keys per node): the first three keys fill the root; 4040 overflows it and 3030 goes up into a new root. 7070 makes [40,50,60][40, 50, 60] overflow and 6060 moves up; 2525 overflows [10,15,20][10, 15, 20] and 2020 moves up. The final tree has the root [20,30,60][20, 30, 60] and the leaves [10,15][10, 15], [25][25], [40,50][40, 50], [70][70].

Deletion

  1. Find the key. If it is in an inner node, replace it by its predecessor (the largest key of the left subtree; the successor works too) and delete that key from its leaf.
  2. Remove the key from the leaf. If the leaf still has at least ⌈m/2⌉−1\lceil m/2 \rceil - 1 keys, stop.
  3. Otherwise the node underflows. If a sibling has a spare key, borrow through the parent: the separator in the parent moves down into the node and the sibling's nearest key moves up into the parent. If no sibling has a spare key, merge the node with a sibling and the separator between them; the parent loses a key and may underflow in turn.
  4. If the root loses its last key, its only child becomes the new root: the tree gets one level lower.

Deletions from a B tree of order 4: start — start

Real tree, not typed
203060101525405070

Outlined node = the node the step works on; shaded = created in this step; bold key = the key being searched or moved.

Step 0

A B tree of order 4 built from the keys 10, 20, 30, 40, 50, 60, 70, 15, 25. A non-root node must keep at least 1 key. When a deletion leaves a node with too few keys, it borrows from a sibling or merges with it.

0 / 13
computed by script: the file and index sizes are computed from their formulas, the trees are built by real insertions and deletions. python3 scripts/gen-db4-traces.py

On the tree above (order 44, minimum 11 key per non-root node) the deletions 25,15,10,40,2025, 15, 10, 40, 20 show borrowing from a sibling, replacement of an inner key by its predecessor, and two merges (the parent loses a key each time).

Capacity and height

For a B tree of order mm with hh levels (the root is level 11) and c=⌈m/2⌉c = \lceil m/2 \rceil:

  • maximum number of keys: every node full: mh−1m^h - 1;
  • minimum number of keys: root with 22 children (1 key), every other node with cc children (c−1c - 1 keys): 2ch−1−12c^{h-1} - 1.

How many keys a B tree holds: start

Computed by a script, not typed
order1 level2 levels3 levels4 levels
order m = 3
order m = 4
order m = 5
order m = 6
order m = 7

Step 0

Minimum: the root has 2 children and every other node the fewest allowed; maximum: every node full. The extreme trees were built node by node and passed the same structural checks as trees built by insertion.

0 / 5
computed by script: the file and index sizes are computed from their formulas, the trees are built by real insertions and deletions. python3 scripts/gen-db4-traces.py

Consequently a tree with nn keys has height hh with ⌈log⁡m(n+1)⌉ ≤ h ≤ ⌊log⁡cn+12⌋+1.\lceil \log_m (n + 1) \rceil \ \le\ h\ \le\ \Big\lfloor \log_{c} \tfrac{n + 1}{2} \Big\rfloor + 1. The extreme trees (completely full, and the leanest possible) were built node by node for orders 33 to 77 and heights up to 44 and have exactly these numbers of keys; random insertions and deletions never left the bounds. For the order 2424 above: a 33-level tree holds between 2⋅122−1=2872 \cdot 12^2 - 1 = 287 and 243−1=13 82324^3 - 1 = 13\,823 keys.

Worked examples

EX 1Order of a B tree

Easy

B=512B = 512 bytes, key 99, tree pointer 66, record pointer 77: p⋅6+(p−1)(9+7)≤512p \cdot 6 + (p - 1)(9 + 7) \le 512, i.e. 22p≤52822p \le 528, so the order is p=24p = 24: at most 2424 children and 2323 keys per node.

EX 2Maximum and minimum keys

Easy

Order 55: at most 55 children and 44 keys per node; a non-root node has at least ⌈5/2⌉=3\lceil 5/2 \rceil = 3 children, so at least 22 keys. Height 22: at most 52−1=245^2 - 1 = 24 keys, at least 2⋅3−1=52 \cdot 3 - 1 = 5 keys. Height 33: between 2⋅32−1=172 \cdot 3^2 - 1 = 17 and 53−1=1245^3 - 1 = 124 keys.

EX 3Insertion with a double split

Medium

Order 44. In the tree with root [30,60][30, 60] and leaves [10,20][10, 20], [40,50][40, 50], [70,80,90][70, 80, 90], inserting 100100 overflows the last leaf [70,80,90,100][70, 80, 90, 100]: the median (position 22) is 9090, which moves up. The parent becomes [30,60,90][30, 60, 90] (3 keys, allowed). Inserting another key into a full parent splits it too and, if it is the root, raises the height.

EX 4Deletion with borrowing and merging

Medium

Order 44 (a non-root node needs at least 11 key). From a node [25][25] whose key is deleted, the leaf is empty: its left sibling [10,15][10, 15] has a spare key, so 1515 moves up to the parent and the separator 2020 comes down: the leaves become [10][10] and [20][20]. If the sibling had only the minimum number of keys, the two leaves and the separator would be merged into one node instead and the parent would lose a key.

EX 5Typical questions

GATE level

(a) "The maximum number of keys in a B tree of order mm and height hh:" mh−1m^h - 1. (b) "Minimum number of keys in a B tree of order mm with hh levels:" 2⌈m/2⌉h−1−12\lceil m/2 \rceil^{h-1} - 1. (c) "The minimum number of children of an internal non-root node of a B tree of order mm:" ⌈m/2⌉\lceil m/2 \rceil. (d) "Why are all leaves on one level?" Splits and merges happen at the top, so the depth of all leaves grows or shrinks together. (e) "Number of disk accesses for a search in a B tree with nn keys:" at most the height. (f) "Which of a B tree and a binary search tree is better for a disk-resident index?" The B tree, because of the large fan-out.

Cheat box

Cheat box

  • Order mm: ≤m\le m children, ≤m−1\le m - 1 keys; non-root ≥⌈m/2⌉\ge \lceil m/2 \rceil children; root ≥2\ge 2 children; all leaves at one level.
  • Order from block size: p=⌊(B+V+Pr)/(P+V+Pr)⌋p = \lfloor (B + V + P_r)/(P + V + P_r) \rfloor.
  • Insert at a leaf; split an overflowing node around the median, median goes up; root split adds a level.
  • Delete (replace by predecessor if inner); fix underflow by borrowing through the parent or merging; root empties: height decreases.
  • Keys in [ 2⌈m/2⌉h−1−1, mh−1 ][\,2\lceil m/2 \rceil^{h-1} - 1,\ m^h - 1\,] for height hh.

Traps and GATE patterns

Connections

A B tree is the balanced multiway version of the binary search tree (BST); the dynamic alternative that keeps all keys in the leaves is the B+ tree, the usual index of database systems (B+ trees); it replaces static multilevel indexes (ordered indexes).

PYQs

2 previous year questions from your book. I re-solved each one myself (2 by running or simulating, 0 by reasoning only); the book's key is shown next to my answer. All PYQs

GATE 2008 · MCQQ20 · page 425 of the book
A B-tree of order 4 is built from scratch by 10 successive
insertions. What is the maximum number of node
splitting operations that may take place?
  • (a)3
  • (b)4
  • (c)5
  • (d)6
GATE 2005 · MCQ · 1 markQ29 · page 426 of the book
A B-Tree used as an index for a large database table has
four levels including the root node. If a new key is
inserted in this index, then the maximum number of nodes
that could be newly created in the process are:
  • (a)5
  • (b)4
  • (c)3
  • (d)2 DATABASE MANAGEMENT SYSTEM

Self-check

Question 1

What is the minimum number of keys in a B tree of order 66 with 33 levels?

Show answer

c=3c = 3: 2⋅32−1=172 \cdot 3^2 - 1 = 17.

Question 2

A B tree of order 44 is empty. Insert 1,2,3,41, 2, 3, 4. What is the root?

Show answer

After 44 the leaf [1,2,3,4][1, 2, 3, 4] overflows; the median at position 22 is 33, so the root is [3][3] with the leaves [1,2][1, 2] and [4][4].

Question 3

Why can the root have fewer than ⌈m/2⌉\lceil m/2 \rceil children?

Show answer

The root has no sibling to borrow from or merge with; it may have as few as 22 children (or be a leaf).

Question 4

How many block reads does a search in a B tree of height 44 need at most?

Show answer

44 (one per level).

Revision card

One-screen revision

  • Order = max children; non-root ≥⌈m/2⌉\ge \lceil m/2 \rceil children; leaves on one level.
  • Insert: leaf, split at median, median up; delete: predecessor, borrow or merge.
  • Keys per height hh: 2⌈m/2⌉h−1−12\lceil m/2 \rceil^{h-1} - 1 to mh−1m^h - 1.
  • p=⌊(B+V+Pr)/(P+V+Pr)⌋p = \lfloor (B + V + P_r)/(P + V + P_r) \rfloor.

References

  • ·Silberschatz, Korth, Sudarshan, Database System Concepts (7th edition), Chapter 14, Section 14.3 (B+-tree index files; B-tree indices are in 14.4)
  • ·Elmasri, Navathe, Fundamentals of Database Systems (7th edition), Chapter 17, Section 17.3 (dynamic multilevel indexes using B-trees and B+-trees)
  • ·Cormen, Leiserson, Rivest, Stein, Introduction to Algorithms (3rd edition), Chapter 18 (B-trees; uses the minimum degree t instead of the order)

Verification

scripts/enum-checks/db_index_check.py: the B tree implemented in scripts/db_tree.py was run through 1500 random insert / delete sequences (orders 3 to 8) and after every operation every structural rule was checked (keys sorted and unique, between ceil(m/2) - 1 and m - 1 keys in a non-root node, root with at least 2 children, all leaves on one level, in-order contents equal a reference set); the number of keys always lay between 2 ceil(m/2)^(h-1) - 1 and m^h - 1; a completely full tree and a leanest tree were built node by node for orders 3 to 7 and heights up to 4 and have exactly the maximum and the minimum number of keys; the order formula is the largest p with p tree pointers and p - 1 (key, record pointer) pairs in a block; the animations are snapshots of the same implementation and replay to the expected contents. CONVENTIONS: this page uses the order m = maximum number of children; Cormen et al. use the minimum degree t (max 2t - 1 keys), Knuth uses the order as the maximum number of children and some books count the pointers of a node differently, and an overflow with an even order can be split with either half one key larger; the choice used here (median at position m // 2) is stated and affects only the shape of trees, not the formulas. Textbook chapter references are from memory. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and re-derived against the textbook definitions and the sample database. No defect found. Status raised to self-checked; verified needs a textbook or human check.

last verified: never