B Trees
B Trees
It designed to work on magnetic disks or other (direct access) secondary storage devices.
h ≤ logt (n+1)/2
• Proof:
– The root contains at least one key
– All other nodes contain at least t-1 keys.
– There are at least 2 nodes at depth 1, at least 2t nodes at depth 2, at least 2ti-1
nodes at depth i and 2th-1 nodes at depth h
Complexity=
O(t)
Complexity=
O(logt n)
ACDE JK NO RSTUV YZ
Insert B
GMPX
ABCDE JK NO RSTUV YZ
ABCDE JK NO RSTUV YZ
Insert Q
GMPTX
ABCDE JK NO QRS UV YZ
ABCDE JK NO QRS UV YZ
Insert L
GM TX
GM TX
Insert F
CGM TX
26
Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Note: T = 3
not a leaf
…k…
y z
x x
not a leaf … k … … k´…
y y
t keys t keys
pred
k´ of k
L
Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal
Deletion Cases (Continued)
Subcase B: Symmetrically, if the child z that follows k in
node x has at least t keys then find successor k´ of k in
subtree rooted at z, recursively delete k´and replace k by k´
in x.
x x
not a leaf … k … … k´…
z z
t keys t keys
succ
k´ of k
x x
not a leaf … k … not a leaf … …
C L
D E G J K
Subcase A: ci[x] has t–1 keys, some sibling has at least t keys.
recursively
x descend x
not a leaf … k1 … … k2 …
ci[x] k2 … ci[x] k1 …
t–1 keys t keys
k k
recursively
descend x
x
not a leaf … k1 … … …
ci[x]
ci[x] ci+1[x] ci[x]’s keys, k1 , ci+1[x]’s keys
t–1 keys t–1 keys 2t–1 keys
k k
C L
D E G J K
2 7 9 15 22 31 43 56 69 72
7 9 15 22 31 43 56 69 72
7 9 15 22 31 43 69 72
Too few keys!
Delete 72
7 9 15 22 31 43 56 69
12 29
Demote root key and
promote leaf key
7 9 15 22 31 43 56 69
Delete 22
12 31
7 9 15 29 43 56 69