B Tree-2020
B Tree-2020
Husam A. Halim
2020
A B
• A B+-tree of order P:
– Root has between 2 and P pointers (unless it’s a leaf).
– Internal nodes have between ⌊(P-1)/2⌋ and P-1 keys, and
#pointers = #keys + 1.
– Leaf nodes have between ⎡(P-1)/2⎤ and P-1 keys.
A B+-Tree of order p = 4
• Internal nodes : ⌊(P-1)/2⌋ ≤ #keys ≤ P-1 1 ≤ #keys ≤ 3
• Leaf nodes : ⎡(P-1)/2⎤ ≤ #keys ≤ P-1 2 ≤ #keys ≤ 3
Hussam A. Halim Computer Science De
pt. 2020
B+-Tree : Insertion
• Find correct leaf L and place the key in L in sorted order.
Insert 1
8
5
5 1 18 55 88
Case2
Case
: overflow
1 : Leaf (new
not full
level) !
Hussam A. Halim Computer Science De
pt. 2020
Example : A B+ Tree of order P = 3. Insert : 8 5 1 7 3 12 9 6 15
=> Max # of keys in a node = P – 1 = 2
Insert 1
7
3
3 5
5
3 1 1 53 15 88 7
CaseCase
2 : Leaf overflow
1 : Leaf (split)
not full
Hussam A. Halim Computer Science De
pt. 2020
Example : A B+ Tree of order P = 3. Insert : 8 5 1 7 3 12 9 6 15
=> Max # of keys in a node = P – 1 = 2
Insert 12 3 5 8 8
3 1 5 812 78 7 8
812
Case 3 : Case
Non-Leaf
2 : Leaf
overflow
overflow
(new level)
Hussam A. Halim Computer Science De
pt. 2020
Example : A B+ Tree of order P = 3. Insert : 8 5 1 7 3 12 9 6 15
=> Max # of keys in a node = P – 1 = 2
Insert
Insert 12
9
6 5
3 8
3 1 5 88 77 6 12 9
Insert 6 5
3 7 88
3 1 5 7 6 88 12 9
Insert 15
5
3 7 8
7 8 12
3 1 5 7 6 8 12
15 12
99 9 15
Case
Case
3 :2Non-Leaf
: Leaf overflow
overflow
Hussam A. Halim Computer Science De
pt. 2020
Example : A B+ Tree of order P = 3. Insert : 8 5 1 7 3 12 9 6 15
=> Max # of keys in a node = P – 1 = 2
Insert 15
5 8
3 7 12
3 1 5 7 6 8 12 9 15
Delete 6
7
1 6
5 9
1 5 6 7 8 9 12
Delete 12
7
1 5 8
9
1 5 7 8 9 129
Delete 9
7
1 5 8
1 5 7 8 9
Case 4 : Case
non-leaf is underflow
3 : underflow (redistribute)
(merge)
Hussam A. Halim Computer Science De
pt. 2020
Example : A B+ Tree of order P = 3. Delete : 5 12 9 8
=> Min # of keys in a leaf = ⌊(P-1)/2⌋ = 1
=> Min # of keys in a non-leaf = ⎡(P-1)/2⎤ = 1
Delete 9 7
1 5 7
1 5 7 8
7 8
Delete 8
9
5
1 7
1 5 7 8
Case Case
4 : non-leaf
3 : underflow
is underflow
(merge)
(merge)
Hussam A. Halim Computer Science De
pt. 2020
Example : A B+ Tree of order P = 3. Delete : 5 12 9 8
=> Min # of keys in a leaf = ⌊(P-1)/2⌋ = 1
=> Min # of keys in a non-leaf = ⎡(P-1)/2⎤ = 1
Delete 8 5
1 1 5
1 5 7
– # of levels = ⌈log20(1000)⌉ = 4