algoritmauzmanı
Sign in
TREN
7.4Other Balanced Trees
Lesson 7.4.3

B-trees: multiway search trees

In the trees so far every node held one key and two children. When the data lives on disk this is expensive: every node needs its own disk read, and with a million records a path is 20 nodes long. A fits hundreds of keys into one node and keeps the tree very short. This lesson covers the rules of the B-tree and how a node splits during insertion.

What you will learn
  • The node rules of a B-tree of order m
  • How a full node splits at its median key and the tree grows from the top
  • Why databases and file systems use B-trees
8 min

1Rules

In a B-tree of order m each node holds at most m children and m - 1 keys. The keys in a node are sorted and divide the children into ranges: in the node [30 60] the first child holds keys smaller than 30, the second keys between 30 and 60, and the third keys larger than 60. This is the multiway form of the rule.

  • Every inner node except the root has at least ⌈m/2⌉ children; nodes stay at least half full.
  • If the root is not a leaf, it has at least 2 children.
  • A node with k children holds k - 1 keys.
  • All leaves are on the same level. Balance comes from this rule, not from rotations.

For example, in a tree of order 5 a node holds at most 4 keys and 5 children, and inner nodes other than the root have at least 3 children (2 keys). Exams sometimes give the degree as the minimum number of children (t); with that definition a node holds at most 2t - 1 keys. Check which definition the question uses.

Search starts at the root. If the value is among the sorted keys of the node, you are done. If not, you see which two keys the value falls between and go down to that range's child. If it is not in a leaf either, the value is not in the tree. Searching inside a node works as in arrays; for a large node binary search is used.

3Insertion: split when full

A new key is always added to a leaf: you go down as in a search and place the key in its sorted spot in the leaf. If the leaf has room, you are done. If the leaf overflows, that is it has m keys, the node splits in two at its median key: the keys left of the median form one node, those on the right another, and the median moves up into the parent. If the parent overflows, it splits too. If splitting reaches the root, the root splits and a new root with one key appears.

That is why a B-tree grows from the top: the depth of the leaves always stays the same, and the tree gets one level taller only when the root splits. This is how all leaves stay on the same level.

Example: 10, 20, 30, 40, 50, 60, 70, 80, 25, 35, 45, 15, 5 are inserted in order into an empty B-tree of order 5 (at most 4 keys per node). The tree is written level by level; "/" moves down one level.

InsertedWhat happenedTree
10, 20, 30, 40The root is a single node and also a leaf; the keys stay sorted.[10 20 30 40]
50The node now has 5 keys and the limit is 4. The median 30 moves up and the node splits in two. A new root appears: the tree grew one level.[30] / [10 20] [40 50]
60, 70They go into the right leaf: [40 50 60 70]. It fits.[30] / [10 20] [40 50 60 70]
80The right leaf overflows: [40 50 60 70 80]. The median 60 moves up to the root.[30 60] / [10 20] [40 50] [70 80]
25, 35, 45, 15Each goes into the leaf for its range; no split.[30 60] / [10 15 20 25] [35 40 45 50] [70 80]
5The left leaf overflows: [5 10 15 20 25]. The median 15 moves up; the root now has 3 keys, which fits.[15 30 60] / [5 10] [20 25] [35 40 45 50] [70 80]

In a split the median is the middle one of the m overflowing keys. If m is odd there is one median; if m is even, books pick the left or the right middle key. Say which one you pick in an exam.

4Deletion in short

Deletion also ends in a leaf. A key deleted from an inner node is replaced by its predecessor or successor, as in a BST. If a leaf drops below the minimum, it first borrows a key from a sibling (the key rotates through the parent); if the sibling is at the minimum too, the two nodes are merged with the separating key from the parent. If a merge empties the root, the tree gets one level shorter.

5Why so short?

Every inner node has at least ⌈m/2⌉ children, so the number of nodes grows by at least that factor per level. The height of a B-tree with n keys is bounded by about the base-⌈m/2⌉ logarithm of n. In real systems one node is sized to one disk block and m is in the hundreds. With m = 200 a billion keys fit in at most 5 levels; the root and the level below usually stay in memory, so a search needs only a few disk reads.

StructureKeys per nodeApproximate height for 1,000,000 keys
Balanced binary tree (AVL, red-black)120
B-tree of order 52-49-13
B-tree of order 20099-1993-4

Most databases keep their indexes in a B+ tree, a kind of B-tree: all records are in the leaves, inner nodes only guide the way, and the leaves are chained in a linked list so range queries read the leaves in order. MySQL's InnoDB engine and many file systems use this structure.

6Try it

At most how many keys does a node of a B-tree of order 7 hold?

Show the answer

The answer is B. In a B-tree of order m a node holds at most m children and m - 1 keys: 7 - 1 = 6.

Which operation makes a B-tree taller?

Show the answer

The answer is B. A leaf split sends the median up to the parent; the depth of the leaves does not change. The tree gets one level taller only when the root splits and a new root appears.

Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.