B-Tree Insert and Delete
B-Tree Insert and Delete
B-Tree Insert and Delete
Constructing a B-tree
• Suppose we start with an empty B-tree and keys arrive in the
following order:1 12 8 2 25 6 14 28 17 7 52 16 48 68 3
26 29 53 55 45
• We want to construct a B-tree of order 5
• The first four items go into the root:
1 2 8 12
2
1
Constructing a B-tree (contd.)
8
2
2
7, 52, 16, 48 get added to the leaf nodes
5
6
1
4 8 17
2
8
1
7
7 1 2 76 12 14 16 25 28 52
48
5
2
1
6
4
8
6
8
1
2
1
Constructing a B-tree (contd.)
8
2
2 Adding 68 causes us to split the right most leaf,
5
6 promoting 48 to the root
1
4
2
8
1 8 17
7
7
5
2 1 2 6 7 12 14 16 25 28 48 52 68
1
6
4
8
6
8
1
2
1
Constructing a B-tree (contd.)
8
2
2 Adding 3 causes us to split the left most leaf
5
6
1 8 17 48
4
2
8
1
7 1 2
3 6 7 12 14 16 25 28 52 68
7
5
2
1
6
4
8
6
8
1
2
1
Constructing a B-tree (contd.)
8
2
2 Add 26, 29, 53, 55 then go into the leaves
5
6
1 3 8 17 48
4
2
8
1
7 1 2 6 7 12 14 16 25262829 52536855
7
5
2
1
6
4
8
6
8
1
2
1
Constructing a B-tree (contd.)
8
2 Exceeds Order.
2 Add 45 increases the trees level Promote middle and
5
split.
6
1
4
2 Exceeds Order.
8 Promote middle and
1
3 8 17 48 split.
7
7
5
2 1 2 6 7 12 14 16 25 26 28 29 45 52 53 55 68
1
6
4
8
6
8
Exercise in Inserting a B-Tree
• Insert the following keys to a 5-way B-tree:
• 3, 7, 9, 23, 45, 1, 5, 14, 25, 24, 13, 11, 8, 19, 4,
31, 35, 56
Answer to Exercise
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