Why B-Trees?
5
Possible Solutions To Unbalanced
Trees
Periodically balance the tree
The basic I/O operation reads and writes blocks rather than
single bytes at a time between secondary storage and memory.
Nodes in
k1 k2 k3 k4 k5
a multiway
tree
* B-Trees can
h k k* n . . . . support duplicate
keys
0 n-1
The Six Rules Governing B-Trees (cont)
R4: The number of subtrees below a non-leaf node is
always one more than the number of entries in the
node.
0 1 2 3
4 entries in a 45 55 67 82
non-leaf node
Keys > 82
Keys < 45
subtree 4
Keys > 45 Keys > 67
subtree 0 & < 55 & < 82 subtree 3
Keys > 55
subtree 1
& < 67
5 subtrees subtree 2
The Six Rules Governing B-Trees (cont)
R5: For any non-leaf node:
An entry at index i is greater than all the
entries in subtree i of the node, and
An entry at index i is less than all the entries
at entry i+1 of the node.
20 50 60 90
10 25 35 40 55 72 82 85 95
Searching For A Target In B-Trees
Start with root node and search for target in the array
at that node. If found, then done and return success.
If the target is not in the root and there are no
children, then also done, but return failure.
If the target is not in the root node, and there are
children, then if the target exists, then it can only be
in one subtree.
Compare the target with the listed keys and traverse
first subtree i for which target is < key_array[i]
… while search key_array from left to right … up to
data_count.
Repeat the process at the new root node
Inserting Into A B-Tree
Overflow?
Yes No
12 19 22
4
Insert 18
6 | 17 Excess Entry
(problem child)
12 18 | 19 | 22
4
Contnd.
MIN = 1 Split problem child, and
6, 17, 19 promote middle key to
MAX = 2
parent node. Still have
excess.
18 22
4 12
18 22
4 12
Insert In Class Exercise
MIN = 1
Insert 5, then insert 7 and MAX = 2
15.
6 17
12 19 22
4
Deleting From A B-Tree
Deletion (cont.)
Case 1: The key is in a leaf , which has more than
the minimum number of keys. If subset[i] has extra
entries, then just delete the data
Delete 21 MIN = 2
MAX = 4
6, 17 6, 17
6, 17 6, 15
6, 17 6, 19
6, 17 6
Delete 95
62
25, 45 85,95
62
25, 45 85,97
90 , 92
30,40