0% found this document useful (0 votes)
55 views

Unit-II B-Tree

Uploaded by

anushkamishra545
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Unit-II B-Tree

Uploaded by

anushkamishra545
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Unit-II

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-

• If node x has n[x] keys then x has n[x]+1 childs


@HOD,CSE-2024-25
B-Tree Definition
▪ Tree T is said to be B-tree if it has following properties-
▪ Every node x has following fields-
▪ n[x] ----- number of keys currently stored in x
▪ Keys are stored in increasing order i.e. Key1[x] ≤ key2[x] ≤ …………
▪ if x is an internal node then it contain n[x]+1 pointers i.e. C1[x], c2[x],……….
▪ The keys in sub-tree are stored as k1 ≤ key1[x] ≤ k2 ≤ key2[x]…………….
▪ Every leaf has same depth ( height of tree)
▪ There are lower and upper bounds on number of keys the node can contain. It is a
fixed integer t≥2 called minimum degree of B-tree
▪ Every node other than root must have t-1 keys. So every node other than root has
t child's. Root must have at least one key
▪ Every node contain at most 2t-1 keys. So internal node has at most 2t child's and
the node is full if it contain 2t-1 keys.
@HOD,CSE-2024-25
Insertion Operations on B-Tree

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

2. Continue with the parent:


1. Until no overflow occurs in the parent
2. If the root overflows, split it too, and create a new root 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)

2. If k is present in ‘x’ and ‘x’ is an internal node then-


2a. If child ‘y’ that precedes k in node ‘x’ has t keys then find predecessor k’ of k and recursively delete k’ and
replace k by k’ in x
2b. If child ‘z’ that follows k in node ‘x’ has t keys then find successor k’ of k and recursively delete k’ and replace k
by k’ in x
2c. If child ‘y’ and ‘z’ both has t-1 keys then merge y and z and remove K

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

1) Insert - D, O and U 2) Delete M

@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

B-Tree-created ( T) B-tree-search (x, k)


1. x= Allocate-node() 1. i=1
2. Leaf[x]=true 2. While i<=n[x] and k>key [x]
3. n[x]=0 3. Do i=i+1
4. Disk-write (x) 4. If i<=n[x] and k=key [x]
5. Root[T]=x 5. then return (x,i)
6. If leaf[x] then
Require O(1) time. 7. return NIL
8. Else
9. disk-read(c [x])
10. return B-tree-search(c [x], k)
@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)

▪ B-Tree is also called as m-way search tree ( where m is order)


▪ For order of m B-tree has-
▪ Minimum number of keys= m/2 -1
▪ Maximum number of keys= m-1

▪ 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- 60, 38, 8, 30

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)

• Let h be the height of B-tree.


• The root contain 1 key and other nodes contain t-1 keys (minimum)

Since,
1= node at depth 0
2= nodes at depth 1
4= nodes at depth 2

So, number of nodes in a B-tree of height h is-

@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

You might also like