Binary trees are good when everything is in memory. Databases keep their data on disks, where reading one block takes far longer than thousands of comparisons in memory. A B-tree reduces the number of block reads by storing many keys in each node, which makes the tree very wide and very short.
Why wide trees
If each node holds up to m−1 keys and has up to m children, the height is about logmn instead of log2n. With m=200, a tree of height 3 can hold about 8 million keys, so any key is found with about 3 disk reads.
Definition
For order 5: every node has at most 4 keys; every node except the root has at least ⌈5/2⌉−1=2 keys.
A B-tree of order 5 (Figure 7.11)
[ M ] / \ [ E H ] [ P T X ] / | \ / | | \ [B D] [F G] [I K L] [N O] [Q S] [V W] [Y Z]
All leaves are on level 3, every non-root node has 2 to 4 keys, and the keys in each node separate its children.
Searching
To search for key k in node x with keys x.key1<⋯<x.keyn:
Find the smallest i with k≤x.keyi (linear or binary search inside the node).
If k=x.keyi, the key is found.
Otherwise, if x is a leaf, k is not in the tree; else read child ci (the child to the left of x.keyi, or the last child if k is larger than every key) from disk and search it.
The number of nodes read is at most the height, O(logmn); the work inside each node is O(m).
Key takeaways
B-tree nodes hold many sorted keys, so the tree is wide and shallow: height O(logmn).
Order m: at most m children / m−1 keys; non-root nodes at least ⌈m/2⌉ children / ⌈m/2⌉−1 keys.
All leaves are on the same level.
Search: find the position in the node, then follow one child; few disk reads.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Define a B-tree of order m and state the minimum and maximum number of keys per node for m=5.
Show worked answer
A B-tree of order m is a multiway search tree in which every node has at most m children and m−1 keys in sorted order; a node with k children has k−1 keys that separate the children’s key ranges; every node except the root has at least ⌈m/2⌉ children (if internal) and at least ⌈m/2⌉−1 keys; the root, if not a leaf, has at least 2 children; and all leaves are on the same level. For m=5: at most 4 keys per node; at least 2 keys per non-root node (the root may have just 1).
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-208/advanced-trees/b-trees-structure-and-search/ to read this page.