B Trees
B Trees
B Trees
Course Instructor
Dr. M.K.Kavitha Devi
Prof. / CSE, TCE
B-Trees
1
Large Dataset
B-Trees 5
An example B-Tree
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
B-Trees 6
Constructing a B-tree
1 2 8 12
B-Trees 7
Constructing a B-tree (contd.)
1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45
1 2 12 25
1 2 6 12 14 25 28
B-Trees 8
Constructing a B-tree (contd.)
1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45
Adding 17 to the right leaf node would over-fill it, so we take the
middle key, promote it (to the root) and split the leaf
8 17
1 2 6 12 14 25 28
1 2 6 7 12 14 16 25 28 48 52
B-Trees 9
Constructing a B-tree (contd.)
1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45
Adding 68 causes us to split the right most leaf, promoting 48 to the
root, and adding 3 causes us to split the left most leaf, promoting 3
to the root; 26, 29, 53, 55 then go into the leaves
3 8 17 48
1 2 6 7 12 14 16 25 26 28 29 52 53 55 68
B-Trees 10
Constructing a B-tree (contd.)
17
3 8 28 48
1 2 6 7 12 14 16 25 26 29 45 52 53 55 68
B-Trees 11
Inserting into a B-Tree
B-Trees 12
Exercise in Inserting a B-Tree
B-Trees 13
Removal from 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 we can delete the key and promote the
predecessor or successor key to the non-leaf deleted key’s
position.
B-Trees 14
Removal from a B-tree (2)
B-Trees 15
Type #1: Simple leaf deletion
Assuming a 5-way
B-Tree, as before... 12 29 52
2 7 9 15 22 31 43 56 69 72
B-Trees 16
Type #2: Simple non-leaf deletion
12 29 56
52 Delete 52
7 9 15 22 31 43 56 69 72
B-Trees 17
Type #4: Too few keys in node and
its siblings
12 29 56
7 9 15 22 31 43 69 72
Too few keys!
Delete 72
B-Trees 18
Type #4: Too few keys in node and
its siblings
12 29
7 9 15 22 31 43 56 69
B-Trees 19
Type #3: Enough siblings
12 29
Demote root key and
promote leaf key
7 9 15 22 31 43 56 69
Delete 22
B-Trees 20
Type #3: Enough siblings
12 31
7 9 15 29 43 56 69
B-Trees 21
Exercise in Removal from a B-Tree
B-Trees 22
Analysis of B-Trees
B-Trees 23
Reasons for using B-Trees
B-Trees 24
Comparing Trees
• Binary trees
– Can become unbalanced and lose their good time complexity (big O)
– AVL trees are strict binary trees that overcome the balance problem
– Heaps remain balanced but only prioritise (not order) the keys
• Multi-way trees
– B-Trees can be m-way, they can have any (odd) number of children
– One B-Tree, the 2-3 (or 3-way) B-Tree, approximates a permanently
balanced binary tree, exchanging the AVL tree’s balancing operations
for insertion and (more complex) deletion operations
B-Trees 25