Unit-II B-Tree
Unit-II B-Tree
B-Tree
@HOD,CSE-2024-25
Introduction to B-Tree
▪ It is a height balance tree ( height= O(logn) )
▪ These trees are design to work on direct storage devices such as magnetic disk.
▪ B-Tree is similar to R-B tree but better to minimize I/O operations.
▪ B-Tree differs with R-B tree in terms of branching factor. B-tree has high branching factor ( node has
many childs)
▪ Example-
B-tree-Insert(T, k)
// k is the new key
1. r=Root[T]
2. If n[r]=2t-1 then // node full
3. s=allocate-node()
4. root[T]=s
5. key[s]=false
6. n[s]=0
7. c1[s]=r
8. B-tree-split(s,1,r) // split node r
9. B-tree-Insert-Nonfull(s, k)
10. else
11. B-tree-Insert-Nonfull(r, k)
@HOD,CSE-2024-25
Split condition (Node overflow)
1. Split the node at median position
– Left: the first t-1 values, become a left child node
– Middle: the middle value at position t, goes up to parent
– Right: the last t-1 values, become a right child node
x … 56 98 …. x … 56 68 98 ….
split
y 60 65 68 83 86 60 65 z 83 86
y
@HOD,CSE-2024-25
Insert the following keys into an empty B-Tree-
20, 14,3, 10, 5, 6, 15, 4 ( Assume t=2)
Insert-
Insert-
Insert-
Insert-
Insert-
@HOD,CSE-2024-25
Q. Show the insertion of following keys into an empty B-tree-
F, S, Q, K, C, L, H, T, V, W, M, R, N, P, A, B, X, Y, D, Z, E, G, I (Assume t=3)
Insert- F, S, Q, K, C
Insert- SPILT //
@HOD,CSE-2024-25
Q. Show the insertion of following keys into an empty B-tree-
F, S, Q, K, C, L, H, T, V, W, M, R, N, P, A, B, X, Y, D, Z, E, G, I (Assume t=3)
Insert- SPLIT//W,
M,R,N
Insert- SPILT // P
@HOD,CSE-2024-25
Q. Show the insertion of following keys into an empty B-tree-
F, S, Q, K, C, L, H, T, V, W, M, R, N, P, A, B, X, Y, D, Z, E, G, I (Assume t=3)
Insert-
Insert-
@HOD,CSE-2024-25
B-tree Delete operation
▪ The algorithm delete a key k from node x.
▪ If key k is present in node x then it must contain at least t keys (min. degree)
▪ If key k is present in child of x then x must also contain t keys to descent from x to that node
▪ Root must contain at least 1 key
Steps-
1. If k is present in node ‘x’ and ‘x’ is a leaf node then remove ‘k’ from x ( ‘x’ must have t keys)
3. if K is not present at internal node x, then determine its appropriate sub-tree. If sub-tree contain t-1 keys then-
3a. If ci[x] has t-1 keys then find its sibling with t keys and then move a key down from x into ci[x] , move a key from
ci[x] sibling up to x and then remove k.
3b. If ci[x] and its sibling both has t-1 keys then merge x, ci[x] and its sibling and then remove k.
@HOD,CSE-2024-25
@HOD,CSE-2024-25
Delete- D
Delete- B
@HOD,CSE-2024-25
Delete- C , P & V
@HOD,CSE-2024-25
Consider the following B-tree ( assume t=3
@HOD,CSE-2024-25
Basic operations on B-Tree
• B-tree Create
• B-Tree search
• B-tree Insert
• B-Tree delete
@HOD,CSE-2024-25
Basic operations on B-Tree
B-tree Search
B-Tree create
• Use to search key K. similar to binary search
• Use to create empty B-tree
tree
▪ So if m=5 then –
▪ Any node have max. 4 keys
▪ So the node have 5 childs
@HOD,CSE-2024-25
Q. Insert the following keys into an initially empty B-tree of order 5.
25, 31, 37, 76, 5, 60, 38, 8, 30, 15, 35, 17, 23, 53, 27, 43, 68, 48 ( UPTU 2004-05)
Insert-
25, 31, 37, 76
Insert- 5
Insert- 15
Insert- 35
@HOD,CSE-2024-25
Q. Consider the following 5-ways B-tree ( UPTU 2008)
1. Insert 60
2. Remove 59
3. Remove 50
4. Remove 17
@HOD,CSE-2024-25
Q. Consider the following 5-ways B-tree ( UPTU 2008)
1. Insert 60
@HOD,CSE-2024-25
Q. Consider the following 5-ways B-tree ( UPTU 2008)
2 Remove 59
@HOD,CSE-2024-25
Q. Consider the following 5-ways B-tree ( UPTU 2008)
Remove 50
Remove 17
@HOD,CSE-2024-25
Theorem- if n≥2, then for any n-key B-Tree of height h and minimum degree t ≥2
( UPTU)
Since,
1= node at depth 0
2= nodes at depth 1
4= nodes at depth 2
@HOD,CSE-2024-25
Q. Why don’t we allow minimum degree of B-tree t=1
• According to definition of B-tree every node have at least t-1 keys and root must
have 1 key.
• If we set t=1 , then node contain 0 keys and this will voilet the property of B-tree
@HOD,CSE-2024-25
Q. Find the maximum height of B-tree with 100000 keys and minimum degree 10
(UPTU)
• We know
@HOD,CSE-2024-25
Q. As a function of minimum degree t, what is the maximum number of keys that
can be stored in a B-tree of height h.
Q. Show the result of inserting following keys into an empty B-tree of order 5
C, N, G, A, H, E, K, Q, M, F, W, L, T, Z, D, P, R, X, Y, S.
@HOD,CSE-2024-25