b+ tree
b+ tree
B+ Tree
#include
#include<stdlib.h>
#include<stdbool.h>
struct BPTreeNode {
int *data;
struct BPTreeNode **child_ptr;
bool leaf;
int n;
}*root = NULL, *np = NULL, *x = NULL;
void insert(int a) {
int i, temp;
x = root;
if (x == NULL) {
root = init();
x = root;
} else {
if (x->leaf == true && x->n == 5) {
temp = split_child(x, -1);
x = root;
for (i = 0; i < (x->n); i++) {
if ((a > x->data[i]) && (a < x->data[i + 1])) {
i++;
break;
} else if (a < x->data[0]) {
break;
} else {
continue;
}
}
x = x->child_ptr[i];
} else {
while (x->leaf == false) {
for (i = 0; i < (x->n); i++) {
if ((a > x->data[i]) && (a < x->data[i + 1])) {
i++;
break;
} else if (a < x->data[0]) {
break;
} else {
continue;
}
}
if ((x->child_ptr[i])->n == 5) {
temp = split_child(x, i);
x->data[x->n] = temp;
x->n++;
continue;
} else {
x = x->child_ptr[i];
}
}
}
}
x->data[x->n] = a;
sort(x->data, x->n);
x->n++;
}
int main() {
int i, n, t;
printf("enter the no of elements to be inserted\n");
scanf("%d", &n);
for(i = 0; i < n; i++) {
printf("enter the element\n");
scanf("%d", &t);
insert(t);
}
printf("traversal of constructed tree\n");
traverse(root);
return 0;
}
OUTPUT
enter the no of elements to be inserted
8
enter the element
10
enter the element
20
enter the element
5
enter the element
6
enter the element
12
enter the element
30
enter the element
7
enter the element
17
traversal of constructed tree
5 6 7 10 12 17 20 30