B Trees
B Trees
Trees
B-Tree vs. Binary Search Tree
B-Trees
• B-trees are balanced search trees designed to
work well on magnetic disks or other direct-
access secondary storage devices.
• they are better at minimizing disk I/O operations
• Many database systems use B-trees, or variants
of B-trees, to store information.
• B-tree nodes may have many children, from a
handful to thousands as compared to binary.
• If an internal B-tree node x contains n[x] keys,
then x has n[x] + 1 children
B Tree
Properties of B-Tree
1) All leaves are at same level.(Max. child possible m)
2) A B-Tree is defined by the term minimum degree (child nodes) ‘t’.
(= ceil[m/2] children )
3) Every node except root must contain at least t-1(ceil[m/2] -1)
keys. Root may contain minimum 1 key.
4) All nodes (including root) may contain at most m-1 keys.
5) Number of children of a node is equal to the number of keys in it
plus 1.
6) All keys of a node are sorted in increasing order. The child
between two keys k1 and k2 contains all keys in the range from k1
and k2.
7) B-Tree grows and shrinks from the root which is unlike Binary
Search Tree. Binary Search Trees grow downward and also shrink
from downward.
Example
B Tree Insertion
• Inserted in leaf as per order.
• If Leaf node gets full after insertion then split
the node from middle. And move middle
element to parent node.(as shown)
12
2 3 8 13 27
20
Insert 10
12
2 3 8 10 13 27
We find the location for 10 by following a path from the root using the stored
key values to guide the search.
The search falls out the tree at the 4th child of the 1st child of the root.
The 1st child of the root has room for the new element, so we store it there.
21
Insert 11
12
2 3 8 10 11 13 27
We fall out of the tree at the child to the right of key 10.
But there is no more room in the left child of the root to hold 11.
Therefore, we must split this node...
22
Insert 11 (Continued)
8 12
2 3 10 11 13 27
The m + 1 children are divided evenly between the old and new nodes.
The parent gets one new child. (If the parent become overfull, then it, too, will
have to be split).
23
Remove 8
8 12
2 3 10 11 13 27
Removing 8 might force us to move another key up from one of the children. It
could either be the 3 from the 1st child or the 10 from the second child.
However, neither child has more than the minimum number of children (3), so
the two nodes will have to be merged. Nothing moves up.
24
Remove 8 (Continued)
12
2 3 10 11 13 27
The root contains one fewer key, and has one fewer child.
25
Remove 13
12
2 3 10 11 13 27
26
Remove 13 (Cont)
11
2 3 10 12 27
27
Remove 11
11
2 3 10 12 27
28
Remove 11 (Cont)
10
2 3 12 27
29
Remove 2
10
2 3 12 27
30
Remove 2 (Cont)
3 10 12 27
The result is illegal, because the root does not have at least 2 children.
Therefore, we must remove the root, making its child the new root.
31
Remove 2 (Cont)
3 10 12 27
32
Insert 49
3 10 12 27
33
Insert 49 (Cont)
3 10 12 27 49
Adding this key make the node overfull, so it must be split into two.
But this node was the root.
So we must construct a new root, and make these its children.
34
Insert 49 (Cont)
12
3 10 27 49
35
Construct B Tree with Following data:
50,80,10,20,60,70,75,90,95,4,5,6,14,23,27,
15,16,51,52,64,68,65,72,78,77,79,73,92,93
,110,100,111,81,82,89
50,80,10,20,60,70,75,90,95,4,5,6,14,2
3,27,15,16,51,52,64,68,65,72,78,77,79
,73,92,93,110,100,111,81,82,89
Insert 50,80,10,20
10 20 50 80
Insert 60
10 20 50 60 80
Split at 50
50
10 20 60 80
50,80,10,20,60,70,75,90,95,4,5,6,14,
23,27,15,16,51,52,64,68,65,72,78,77,7
9,73,92,93,110,100,111,81,82,89
50
Insert 70,75
10 20 60 70 75 80
Insert 90 60 70 75 80 90
50 75
10 20 60 70 80 90
• Insert 95,4,5
50 75
4 5 10 20 60 70 80 90 95
• Insert 6
6 50 75
10 20 60 70 80 90 95
4 5
6 50 75
• Insert 14,23
10 14 20 23 60 70 80 90 95
4 5
23 27 60 70 80 90 95
4 5 10 14
• Insert 15,16,51,52 6 20 50 75
23 27 51 52 60 70 80 90 95
4 5 10 14 15 16
51 52 64 70
• Insert 64 using split(2 level split required)
50
6 20 60 75
4 5 51 52 64 70 80 90 95
10 14 15 16 23 27
• Insert 68,65
50
6 20 60 75
4 5 23 27 51 52 64 65 68 70 80 90 95
10 14 15 16
50
• Insert 72,77
6 20 60 68 75
4 5 23 27 51 52 77 80 90 95
10 14 15 16
64 65 70 72
50
• Insert 79
6 20 60 68 75 80
4 5 23 27 51 52 77 79 90 95
10 14 15 16
64 65 70 72
50
• Insert 73,92,93
6 20 60 68 75 80
77 79 90 92 93 95
4 5 23 27 51 52
10 14 15 16
64 65 70 72 73
50
90 92 95 110
4 5 10 14 15 16 23 27 51 52 77 79
64 65 70 72 73
Resolving 93
• Insert 110 , Resolve 93
50 75
,
6 20 60 68 80 93
90 92 95 110
10 14 15 16 23 27 51 52 77 79
4 5
64 65 70 72 73
• Insert100, 111
50 75
,
6 20 60 68 80 93
10 14 15 16 23 27 51 52 77 79
4 5
64 65 70 72 73
• Insert 81,82,89
50 75
,
6 20 60 68 80 89 93
95 100 110 111
4 5
10 14 15 16 23 27 51 52 77 79
81 82 90 92
64 65 70 72 73
Delete 64,23,72, 65, 20,70,95, 77, 80,
100, 6,27,60,16,50,