Advanced Data Structures: B-Trees
Advanced Data Structures: B-Trees
B-Trees
SOORAJ KUMAR
07-0113-CS
B-Trees
1
Contents
• Idea
• Definition
• Construction
• Insertion
• Deletion
• Analysis of B-trees
• Time analysis
B-Trees 2
Why do we use B-trees
B-Trees 3
Factors that affect the time
Many factors can effect the speed of accessing data within the
secondary memory
• The moving of arms on the platters
• Finding the track and sector on the secondary memory’s
platters
• Read and write time
B-Trees 4
B-Trees 5
Definition of a B-tree
B-Trees 6
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 7
Constructing a B-tree
1 2 8 12
B-Trees 8
Constructing a B-tree (contd.)
1 2 12 25
1 2 6 12 14 25 28
B-Trees 9
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 10
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 11
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 12
Inserting into a B-Tree
B-Trees 13
Inserting into a B-Tree
B-Trees 14
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 15
Removal from a B-tree (2)
B-Trees 16
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 17
Type #2: Simple non-leaf deletion
12 29 56
52 Delete 52
7 9 15 22 31 43 56 69 72
B-Trees 18
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 19
Type #4: Too few keys in node and
its siblings
12 29
7 9 15 22 31 43 56 69
B-Trees 20
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 21
Type #3: Enough siblings
12 31
7 9 15 29 43 56 69
B-Trees 22
Algorithm for deletion
B-Trees 23
Continued…
B-Trees 24
Analysis of B-Trees
B-Trees 25
B-tree search runtime
B-Trees 26
Time Complexity of Operations in B-tree
B-Trees 27
Reasons for using B-Trees
B-Trees 28
Thanks
B-Trees 29