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 , 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 (maximum number of children, ) is a rooted tree such that:
- every node has at most children and at most keys;
- every node except the root and the leaves has at least children, so every non-root node has at least keys;
- the root has at least children unless it is a leaf;
- a node with keys has children (if it is not a leaf), and all keys in the -th subtree lie between and ;
- 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 tree pointers holds keys and record pointers. With block size , tree pointer , key and record pointer : the node must satisfy , so Example: , , , : .
How it works
Search
Start at the root. In a node, find the first key 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 nodes (blocks) are read, where is the height; a search may stop earlier at an inner node.
Insertion
- Search for the key; if present, stop (keys are unique).
- The key goes into the leaf where the search ended (a B tree grows at the leaves).
- If the node now has keys (one more than allowed) it overflows: split it around its median key (position , counting from ); 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.
- 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 typedOutlined 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.
Inserting into an empty tree of order (at most keys per node): the first three keys fill the root; overflows it and goes up into a new root. makes overflow and moves up; overflows and moves up. The final tree has the root and the leaves , , , .
Deletion
- 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.
- Remove the key from the leaf. If the leaf still has at least keys, stop.
- 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.
- 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 typedOutlined 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.
On the tree above (order , minimum key per non-root node) the deletions 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 with levels (the root is level ) and :
- maximum number of keys: every node full: ;
- minimum number of keys: root with children (1 key), every other node with children ( keys): .
How many keys a B tree holds: start
Computed by a script, not typed| order | 1 level | 2 levels | 3 levels | 4 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.
Consequently a tree with keys has height with The extreme trees (completely full, and the leanest possible) were built node by node for orders to and heights up to and have exactly these numbers of keys; random insertions and deletions never left the bounds. For the order above: a -level tree holds between and keys.
Worked examples
EX 1Order of a B tree
Easybytes, key , tree pointer , record pointer : , i.e. , so the order is : at most children and keys per node.
EX 2Maximum and minimum keys
EasyOrder : at most children and keys per node; a non-root node has at least children, so at least keys. Height : at most keys, at least keys. Height : between and keys.
EX 3Insertion with a double split
MediumOrder . In the tree with root and leaves , , , inserting overflows the last leaf : the median (position ) is , which moves up. The parent becomes (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
MediumOrder (a non-root node needs at least key). From a node whose key is deleted, the leaf is empty: its left sibling has a spare key, so moves up to the parent and the separator comes down: the leaves become and . 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 and height :" . (b) "Minimum number of keys in a B tree of order with levels:" . (c) "The minimum number of children of an internal non-root node of a B tree of order :" . (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 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 : children, keys; non-root children; root children; all leaves at one level.
- Order from block size: .
- 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 for height .
Traps and GATE patterns
Connections
Across subjects
Within this subject
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
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
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 with levels?
Show answerHide
: .
Question 2
A B tree of order is empty. Insert . What is the root?
Show answerHide
After the leaf overflows; the median at position is , so the root is with the leaves and .
Question 3
Why can the root have fewer than children?
Show answerHide
The root has no sibling to borrow from or merge with; it may have as few as children (or be a leaf).
Question 4
How many block reads does a search in a B tree of height need at most?
Show answerHide
(one per level).
Revision card
One-screen revision
- Order = max children; non-root children; leaves on one level.
- Insert: leaf, split at median, median up; delete: predecessor, borrow or merge.
- Keys per height : to .
- .
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
My notes (saved in this browser)