B Trees 130126021111 Phpapp02
B Trees 130126021111 Phpapp02
B Trees 130126021111 Phpapp02
26 A B-tree of order 5
containing 26 items
6 12
42 51 62
1 2 4 7 8 13 15 18 25
27 29 45 46 48 53 55 60 64 70 90
1 2 8 12
• To put the fifth item in the root would violate condition 5
• Therefore, when 25 arrives, pick the middle key to make a
new root
1
12
8 Constructing a B-tree
2
25 Add 25 to the tree
6
14
28
17 Exceeds Order.
7 Promote middle and
52 split.
1 2 8 12 25
16
48
68
3
26
29
1
12
8 Constructing a B-tree (contd.)
2
25
6 8
14
28
17 1 2 12 25
7
52 6, 14, 28 get added to the leaf nodes:
16 8
48
68
3 1 2
1 6
2 12 14
25 28
26
29
1
12
8 Constructing a B-tree (contd.)
2
25
6 Adding 17 to the right leaf node would over-fill it, so we take
14 the middle key, promote it (to the root) and split the leaf
28
8
17
7
52
16 1 2 6
2 25 28 28
12 14 17
48
68
3
26
29
1
12
8 Constructing a B-tree (contd.)
2
25 7, 52, 16, 48 get added to the leaf nodes
6
14
28
8 17
17
7
52
16 1 2 76 12 14
16 25 28 52
48
48
68
3
26
29
1
12
8 Constructing a B-tree (contd.)
2
25
6 Adding 68 causes us to split the right most leaf,
14 promoting 48 to the root
28
17
7
52 8 17
16
48 1 2 6 7 12 14 16 25 28 48 52 68
68
3
26
29
1
12
8 Constructing a B-tree (contd.)
2
25
6 Adding 3 causes us to split the left most leaf
14
28 8 17 48
17
7
52
1 2
3 6 7 12 14 16 25 28 52 68
16
48
68
3
26
29
1
12
8 Constructing a B-tree (contd.)
2
25
6 Add 26, 29, 53, 55 then go into the leaves
14
28 3 8 17 48
17
7
52
1 2 6 7 12 14 16 25262829 52536855
16
48
68
3
26
29
1
12
8 Constructing a B-tree (contd.)
2
25 Exceeds Order.
6 Add 45 increases the trees level Promote middle and
14 split.
28
17 Exceeds Order.
7 Promote middle and
3 8 17 48 split.
52
16
48 1 2 6 7 12 14 16 25 26 28 29 45 52 53 55 68
68
3
26
29
Inserting into a B-Tree
• During insertion, the key always goes into a leaf. For deletion
we wish to remove from a leaf. There are three possible ways
we can do this:
• 1 - If the key is already in a leaf node, and removing it doesn’t
cause that leaf node to have too few keys, then simply remove
the key to be deleted.
• 2 - If the key is not in a leaf then it is guaranteed (by the
nature of a B-tree) that its predecessor or successor will be in
a leaf -- in this case can we delete the key and promote the
predecessor or successor key to the non-leaf deleted key’s
position.
Removal from a B-tree (2)
Assuming a 5-way
B-Tree, as before... 12 29 52
2 7 9 15 22 31 43 56 69 72
12 29 56
52 Delete 52
7 9 15 22 31 43 56 69 72
12 29 56
7 9 15 22 31 43 69 72
Too few keys!
Delete 72
12 29
7 9 15 22 31 43 56 69
12 29
Demote root key and
promote leaf key
7 9 15 22 31 43 56 69
Delete 22
12 31
7 9 15 29 43 56 69
• Rest of slides will talk about the code necessary for the
implementation of a b-tree class