|
| 1 | +#include<iostream> |
| 2 | +using namespace std; |
| 3 | +struct BplusTree { |
| 4 | + int *d; |
| 5 | + BplusTree **child_ptr; |
| 6 | + bool l; |
| 7 | + int n; |
| 8 | +}*r = NULL, *np = NULL, *x = NULL; |
| 9 | +BplusTree* init()//to create nodes { |
| 10 | + int i; |
| 11 | + np = new BplusTree; |
| 12 | + np->d = new int[6];//order 6 |
| 13 | + np->child_ptr = new BplusTree *[7]; |
| 14 | + np->l = true; |
| 15 | + np->n = 0; |
| 16 | + for (i = 0; i < 7; i++) { |
| 17 | + np->child_ptr[i] = NULL; |
| 18 | + } |
| 19 | + return np; |
| 20 | +} |
| 21 | + |
| 22 | +void traverse(BplusTree *p)//traverse tree { |
| 23 | + cout<<endl; |
| 24 | + int i; |
| 25 | + for (i = 0; i < p->n; i++) { |
| 26 | + if (p->l == false) { |
| 27 | + traverse(p->child_ptr[i]); |
| 28 | + } |
| 29 | + cout << " " << p->d[i]; |
| 30 | + } |
| 31 | + if (p->l == false) { |
| 32 | + traverse(p->child_ptr[i]); |
| 33 | + } |
| 34 | + cout<<endl; |
| 35 | +} |
| 36 | + |
| 37 | +void sort(int *p, int n)//sort the tree { |
| 38 | + int i, j, t; |
| 39 | + for (i = 0; i < n; i++) { |
| 40 | + for (j = i; j <= n; j++) { |
| 41 | + if (p[i] >p[j]) { |
| 42 | + t = p[i]; |
| 43 | + p[i] = p[j]; |
| 44 | + p[j] = t; |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +int split_child(BplusTree *x, int i) { |
| 51 | + int j, mid; |
| 52 | + BplusTree *np1, *np3, *y; |
| 53 | + np3 = init(); |
| 54 | + np3->l = true; |
| 55 | + if (i == -1) { |
| 56 | + mid = x->d[2]; |
| 57 | + x->d[2] = 0; |
| 58 | + x->n--; |
| 59 | + np1 = init(); |
| 60 | + np1->l = false; |
| 61 | + x->l = true; |
| 62 | + for (j = 3; j < 6; j++) { |
| 63 | + np3->d[j - 3] = x->d[j]; |
| 64 | + np3->child_ptr[j - 3] = x->child_ptr[j]; |
| 65 | + np3->n++; |
| 66 | + x->d[j] = 0; |
| 67 | + x->n--; |
| 68 | + } |
| 69 | + for (j = 0; j < 6; j++) { |
| 70 | + x->child_ptr[j] = NULL; |
| 71 | + } |
| 72 | + np1->d[0] = mid; |
| 73 | + np1->child_ptr[np1->n] = x; |
| 74 | + np1->child_ptr[np1->n + 1] = np3; |
| 75 | + np1->n++; |
| 76 | + r = np1; |
| 77 | + } else { |
| 78 | + y = x->child_ptr[i]; |
| 79 | + mid = y->d[2]; |
| 80 | + y->d[2] = 0; |
| 81 | + y->n--; |
| 82 | + for (j = 3; j <6 ; j++) { |
| 83 | + np3->d[j - 3] = y->d[j]; |
| 84 | + np3->n++; |
| 85 | + y->d[j] = 0; |
| 86 | + y->n--; |
| 87 | + } |
| 88 | + x->child_ptr[i + 1] = y; |
| 89 | + x->child_ptr[i + 1] = np3; |
| 90 | + } |
| 91 | + return mid; |
| 92 | +} |
| 93 | + |
| 94 | +void insert(int a) { |
| 95 | + int i, t; |
| 96 | + x = r; |
| 97 | + if (x == NULL) { |
| 98 | + r = init(); |
| 99 | + x = r; |
| 100 | + } else { |
| 101 | + if (x->l== true && x->n == 6) { |
| 102 | + t = split_child(x, -1); |
| 103 | + x = r; |
| 104 | + for (i = 0; i < (x->n); i++) { |
| 105 | + if ((a >x->d[i]) && (a < x->d[i + 1])) { |
| 106 | + i++; |
| 107 | + break; |
| 108 | + } else if (a < x->d[0]) { |
| 109 | + break; |
| 110 | + } else { |
| 111 | + continue; |
| 112 | + } |
| 113 | + } |
| 114 | + x = x->child_ptr[i]; |
| 115 | + } else { |
| 116 | + while (x->l == false) { |
| 117 | + for (i = 0; i < (x->n); i++) { |
| 118 | + if ((a >x->d[i]) && (a < x->d[i + 1])) { |
| 119 | + i++; |
| 120 | + break; |
| 121 | + } else if (a < x->d[0]) { |
| 122 | + break; |
| 123 | + } else { |
| 124 | + continue; |
| 125 | + } |
| 126 | + } |
| 127 | + if ((x->child_ptr[i])->n == 6) { |
| 128 | + t = split_child(x, i); |
| 129 | + x->d[x->n] = t; |
| 130 | + x->n++; |
| 131 | + continue; |
| 132 | + } else { |
| 133 | + x = x->child_ptr[i]; |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | +} |
| 138 | + x->d[x->n] = a; |
| 139 | + sort(x->d, x->n); |
| 140 | + x->n++; |
| 141 | +} |
| 142 | + |
| 143 | +int main() { |
| 144 | + int i, n, t; |
| 145 | + cout<<"enter the no of elements to be inserted\n"; |
| 146 | + cin>>n; |
| 147 | + for(i = 0; i < n; i++) { |
| 148 | + cout<<"enter the element\n"; |
| 149 | + cin>>t; |
| 150 | + insert(t); |
| 151 | + } |
| 152 | + cout<<"traversal of constructed B tree\n"; |
| 153 | + traverse(r); |
| 154 | +} |
0 commit comments