B Tree
B Tree
Root
40
20 33 51 63
10 15 20 27 33 37 40 46 51 55 63 97
Properties of B+ Tree
• For i = 1, 2, . . ., n–1, pointer Pi points to a file record with search-key value Ki,
• If Li, Lj are leaf nodes and i < j, Li’s search-key values are less than or equal to
Lj’s search-key values
• Pn points to next leaf node in search-key order
Leaf Nodes in B+ tree
P1 K1 P2 K2 P3
Record of K2
Record of K1
Record of K2
Non Leaf Nodes in B+ tree
P1 K1 P2 K2 P3
S1 S2 S3
Example of B+ Tree
• Order n =6
• Leaf nodes must have between 3 and 5 values
((n-1/2 ) and n –1, with n = 6
• Non-leaf nodes other than root must have between 3 and 6 children
(n/2 and n with n =6
• Root must have at least 2 children
Observations about B+ tress
Searching in B+ tree
• Search records which has the search key value k
• Examine the root and finding the smallest search key (k1) in the node that
is greater than k
• Follow the pointer P1 for the corresponding k1 until reach to the leaf node
• If k is equal to key in node then follow the corresponding pointer to
access the record.
B+ Tree Insertion (1/4)
• Find the appropriate leaf node in which search key would appear
• If node contains key, do nothing. Insert the record with the key into file and add to
bucket a pointer to file
• Otherwise, insert the key in the node (if not full) and position it so that search key
values are sorted in the node and then add the record to file
10 12 13 14 15
• Insert 12
B+ Tree Insertion (2/4)
• If node is full or overflow, i.e. when number of search key values
exceeds n-1. Do the following steps
• Insert 11:
10 11 12 13 14 15
13
10 11 12 13 14 15
B+ Tree Insertion (4/4)
If overflow happens in non-leaf node
• Split the node into two nodes
• 1st node contains (ceil(n/2) -1) values ( n: number of pointer/children)
• Move the smallest of remaining value , together with pointer, to parent
node
• 2nd node contains remaining values
12
10 11 13 14 15
Insert 1, 3, 5, 7, 9, 2, 4, 6, 8, 10
• Insert
1
1
28
Insert 1, 3, 5, 7, 9, 2, 4, 6, 8, 10
1
• Insert 3,
5
29
Insert 1, 3, 5, 7, 9, 2, 4, 6, 8, 10
1
• Insert 3,
5
1 3 5
30
Insert 1, 3, 5, 7, 9, 2, 4, 6, 8, 10
1 3 5
• Insert
7
31
Insert 1, 3, 5, 7, 9, 2, 4, 6, 8, 10
1 3 5
• Insert
7
1 3 5 7
32
Insert 1, 3, 5, 7, 9, 2, 4, 6, 8, 10
5
1 3 5 7
• Insert
9
33
Insert 1, 3, 5, 7, 9, 2, 4, 6, 8, 10
5
1 3 5 7
• Insert
9
5
1 3 5 7 9
34
Insert 1, 3, 5, 7, 9, 2, 4, 6, 8, 10
5
1 3 5 7 9
• Insert
2
35
Insert 1, 3, 5, 7, 9, 2, 4, 6, 8, 10
5
1 3 5 7 9
• Insert
2
5
1 2 3 5 7 9
36
Insert 1, 3, 5, 7, 9, 2, 4, 6, 8, 10
5
1 2 3 5 7 9
• Insert
4
37
Insert 1, 3, 5, 7, 9, 2, 4, 6, 8, 10
5
1 2 3 5 7 9
• Insert
3 5
4
1 2 3 4 5 7 9
38
Insert 1, 3, 5, 7, 9, 2, 4, 6, 8, 10
3 5
1 2 3 4 5 7 9
• Insert
6
3 5 7
1 3 5 7
2 4 6 9
40
Insert 1, 3, 5, 7, 9, 2, 4, 6, 8, 10
3 5 7
1 2 3 4 5 6 7 9
• Insert
8
3 5 7
1 2 3 4 5 6 7 8 9
42
Insert 1, 3, 5, 7, 9, 2, 4, 6, 8, 10
3 5 7
1 2 3 4 5 6 7 8 9
• Insert
10 7
3 5 9
7 8 9 10
1 2 3 4 5 6
44
• Example : To calculate of order p of B+ tree
• Search key field is V = 9 bytes . Block size is B = 512 bytes . Record pointer is Pr = 7 bytes . Block
pointer/tree pointer is P = 6 bytes
• An internal node can have up to p tree pointers and p-1 key fields, these must fit in a single block . Thus,
p * P + (p-1) * V ≤ B
p*6+(p-1)9 ≤ 512 29
15p ≤ 521
p ≤ 34 for intermediate nodes
• The leaf nodes have the same number of values and pointers, except they are data pointers and next
pointer.
• The order of pleaf can be calculated as follows:
Pleaf * (Pr+V) + P ≤ 512
Pleaf * (7+9) + 6 ≤ 512
Pleaf * 16 ≤ 512 - 6
Pleaf * 16 ≤ 506
Pleaf ≤ 31
• Construct a B+ tree on previous example
• Assume each node is 69% full
• p = 34, pleaf = 31
• On the average, each internal node have 0.69 * 34 ~ 23 pointers and 22 key values
• On the average leaf node has 0.69 * 31 ~ 21 data record pointers