B+ Trees
B+ Trees
The B+ trees are extensions of B trees designed to make the insertion, deletion and
searching operations more efficient.
The properties of B+ trees are similar to the properties of B trees, except that the B
trees can store keys and records in all internal nodes and leaf nodes while B+ trees store
records in leaf nodes and keys in internal nodes. One profound property of the B+ tree is
that all the leaf nodes are connected to each other in a single linked list format and a
data pointer is available to point to the data present in disk file. This helps fetch the
records in equal numbers of disk access.
Since the size of main memory is limited, B+ trees act as the data storage for the
records that couldn’t be stored in the main memory. For this, the internal nodes are
stored in the main memory and the leaf nodes are stored in the secondary memory
storage.
Properties of B+ trees
Every node in a B+ Tree, except root, will hold a maximum of m children and (m-
1) keys, and a minimum of ⌈ m2 ⌉ children and ⌈ m−1
2
⌉ keys, since the order of the
tree is m.
The root node must have no less than two children and at least one search key.
All the paths in a B tree must end at the same level, i.e. the leaf nodes must be
at the same level.
They are almost similar to the B tree operations as the base idea to store data in both
data structures is same. However, the difference occurs as the data is stored only in the
leaf nodes of a B+ trees, unlike B trees.
Insertion operation
The insertion to a B+ tree starts at a leaf node.
Step 1 − Calculate the maximum and minimum number of keys to be added onto the
B+ tree node.
Step 2 − Insert the elements one by one accordingly into a leaf node until it exceeds the
maximum key number.
Step 3 − The node is split into half where the left child consists of minimum number of
keys and the remaining keys are stored in the right child.
Step 4 − But if the internal node also exceeds the maximum key property, the node is
split in half where the left child consists of the minimum keys and remaining keys are
stored in the right child. However, the smallest number in the right child is made the
parent.
Step 5 − If both the leaf node and internal node have the maximum keys, both of them
are split in the similar manner and the smallest key in the right child is added to the
parent node.
Example
Open Compiler
void insert(int a) {
int i, t;
x = r;
if (x == NULL) {
r = init();
x = r;
} else {
if (x->l == 1 && x->n == 6) {
t = split_child(x, -1);
x = r;
for (i = 0; i < x->n; i++) {
if (a > x->d[i] && a < x->d[i + 1]) {
i++;
break;
} else if (a < x->d[0]) {
break;
} else {
continue;
}
}
x = x->child_ptr[i];
} else {
while (x->l == 0) {
for (i = 0; i < x->n; i++) {
if (a > x->d[i] && a < x->d[i + 1]) {
i++;
break;
} else if (a < x->d[0]) {
break;
} else {
continue;
}
}
if (x->child_ptr[i]->n == 6) {
t = split_child(x, i);
x->d[x->n] = t;
x->n++;
continue;
} else {
x = x->child_ptr[i];
}
}
}
}
x->d[x->n] = a;
sort(x->d, x->n);
x->n++;
}
int main() {
int i, n, t;
insert(10);
insert(20);
insert(30);
insert(40);
insert(50);
printf("Insertion Done");
printf("\nB+ tree:");
traverse(r);
return 0;
}
Output
Insertion Done
B+ tree:
10 20 30 40 50