B-Trees: Presentation Slides: From Internet Source
B-Trees: Presentation Slides: From Internet Source
B-Trees
1
Motivation for B-Trees
B-Trees 2
Motivation (cont.)
B-Trees 3
Definition of a B-tree
B-Trees 4
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 5
Constructing a B-tree
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
B-Trees 6
Constructing a B-tree (contd.)
1 2 12 25
1 2 6 12 14 25 28
B-Trees 7
Constructing a B-tree (contd.)
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 8
Constructing a B-tree (contd.)
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 9
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 10
Inserting into a B-Tree
B-Trees 11
Exercise in Inserting a B-Tree
B-Trees 12
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 13
Removal from a B-tree (2)
B-Trees 14
Type #1: Simple leaf deletion
Assuming a 5-way 1 2 5
B-Tree, as before...
2 9 2
1 2 3 4 5 6 7
2 7 9
5 2 1 3 6 9 2
B-Trees 15
Type #2: Simple non-leaf deletion
1 2 5 Delete 52
2 9 2
6
1 2 3 4 5 6 7
7 9
5 2 1 3 6 9 2
B-Trees 16
Type #4: Too few keys in node and
its siblings
1 2 5
2 9 6
Join back together
1 2 3 4 6 7
7 9
5 2 1 3 9 2
Too few keys!
Delete 72
B-Trees 17
Type #4: Too few keys in node and
its siblings
1 2
2 9
1 2 3 4 5 6
7 9
5 2 1 3 6 9
B-Trees 18
Type #3: Enough siblings
1 2
2 9
Demote root key and
promote leaf key
1 2 3 4 5 6
7 9
5 2 1 3 6 9
Delete 22
B-Trees 19
Type #3: Enough siblings
1 3
2 1
1 2 4 5 6
7 9
5 9 3 6 9
B-Trees 20
Exercise in Removal from a B-Tree
B-Trees 21
Analysis of B-Trees
B-Trees 22
Reasons for using B-Trees
B-Trees 23
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
• 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 24