-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathB+ Tree.cpp
302 lines (257 loc) · 7.55 KB
/
B+ Tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include<bits/stdc++.h>
#define vi vector<int>
#define vp vector<node*>
#define pb(x) push_back(x)
#define pp() pop_back()
#define MAXN 4
#define LIM 100005
using namespace std;
/* 1 100 1 500 1 1000 1 000 1 600 1 010 1 300 1 2000
1 700 1 800 1 2000 1 025 1 050 1 080 1 1500 1 2500
1 100 1 200 1 300 1 400 1 130 1 160 1 000 1 050
1 5000 1 2500 1 5000 1 3000 */
void instr() {
cout << "PRESS 1 TO INSERT A VALUE." << endl;
cout << "PRESS 2 TO DELETE A VALUE." << endl;
cout << "PRESS 3 TO TRAVERSE DFS WISE." << endl;
cout << "PRESS 4 TO TRAVERSE BFS WISE." << endl;
cout << "PRESS 5 TO SEARCH A VALUE." << endl;
}
struct node {
int sz=0, szp=0;
int val[MAXN+3];
node* ptr[MAXN+3];
node* par;
node* sibling;
int parPtrID;
bool isLeaf;
node* create() {
node* cur = (node*)malloc(sizeof (node));
cur->sz = 0; cur->szp = 0;
cur->isLeaf = true;
cur->par = NULL;
cur->sibling = NULL;
cur->parPtrID = -1;
// cout << "New Tree Created!" << endl;
return cur;
}
void insertVal(int in, int x) {
for(int i=sz-1; i>=in; i--) val[i+1] = val[i];
val[in] = x;
sz++;
}
void insertPtr(int in, node* p) {
for(int i=szp-1; i>=in; i--) {
ptr[i]->parPtrID++;
ptr[i+1] = ptr[i];
// ptr[i]->parPtrID++;
}
ptr[in] = p;
szp++;
}
void pushBackVal(int x) {
val[sz++] = x;
}
void pushBackPtr(node* p) {
ptr[szp++] = p;
}
void eraseVal(int in) {
for(int i=in+1; i<sz; i++) val[i-1] = val[i];
sz--;
}
void erasePtr(int in) {
for(int i=in+1; i<szp; i++) ptr[i-1] = ptr[i];
szp--;
}
void reverseVal() {
reverse(val, val+sz);
}
void reversePtr() {
reverse(ptr, ptr+szp);
}
void insert(int x) {
int iny=0;
while(iny < sz) {
if(x < val[iny]) break;
iny++;
}
if(isLeaf) insertVal(iny, x);
else {
// cout << "INS " << iny << endl;
ptr[iny]->insert(x);
}
if(sz == MAXN) split();
}
void split() {
node* splitedNode = create();
splitedNode->isLeaf = isLeaf;
sibling = splitedNode;
while(sz > MAXN/2) {
splitedNode->pushBackVal(val[sz-1]);
sz--; ///val.pp();
}
while(szp > MAXN/2+1) {
splitedNode->pushBackPtr(ptr[szp-1]);
szp--; ///ptr.pp();
}
splitedNode->reverseVal();
splitedNode->reversePtr();
for(int i=0; i<splitedNode->szp; i++) {
splitedNode->ptr[i]->parPtrID = i;
splitedNode->ptr[i]->par = splitedNode;
}
///notun root
if(par == NULL) {
par = create();
par->isLeaf = false;
par->pushBackPtr(this);
parPtrID = 0;
}
splitedNode->par = par;
splitedNode->parPtrID = parPtrID+1;
// cout << "PARPTR " << parPtrID << endl;
///handle parent er majhkhane insert
par->insertVal(parPtrID, splitedNode->val[0]);
par->insertPtr(parPtrID+1, splitedNode);
///handle leaf na hoile split thk 0 er ta delete
if(!isLeaf) splitedNode->eraseVal(0);
// cout << "AFTER SPLIT" << endl;
// for(int i=0; i<sz; i++) cout << val[i] << ' ' ; cout << endl;
// for(int i=0; i<splitedNode->sz; i++) cout << splitedNode->val[i] << ' ' ; cout << endl;
// for(int i=0; i<par->sz; i++) cout << par->val[i] << ' ' ; cout << endl;
}
void remove(int x) {
printNode();
int iny=sz-1;
while(iny >= 0 && x < val[iny]) {
// if(x < val[iny]) break;
iny--;
}
iny++;
// cout << "GOTO " << iny << endl;
if(isLeaf) {
iny--;
if(iny>=0 && val[iny] == x) {
// if(iny == 0 && parPtrID) par->val[parPtrID-1] = val[iny+1];
eraseVal(iny);
if(sz < MAXN/2) {
if(borrowFromRight()) return;
// if(borrowFromRight()) return;
// redistribute();
}
}
else cout << "VALUE NOT FOUND!" << endl;
}
else {
ptr[iny]->remove(x);
}
}
void search(int x) {
printNode();
int iny=sz-1;
while(iny >= 0 && x < val[iny]) {
iny--;
}
iny++;
if(isLeaf) {
iny--;
if(val[iny] == x) {
cout << "VALUE FOUND!" << endl;
}
else cout << "VALUE NOT FOUND!" << endl;
}
else {
cout << "GOTO: " << iny << endl;
ptr[iny]->search(x);
}
}
bool borrowFromRight() {
if(parPtrID+1 == par->szp) return false; ///right sibling nai
node* rgt = par->ptr[parPtrID+1];
if(rgt->sz) {
}
}
// bool borrowFromLeft() {
// if(parPtrID == 0) return false;
// node* leftSib = par->ptr[parPtrID-1];
// if(leftSib.sz < MAXN/2)
// }
void dfsTraverse() {
printNode();
for(int i=0; i<szp; i++) {
if(ptr[i] != NULL) {
cout << "Goto " << i << " : " << endl; getchar();
ptr[i]->dfsTraverse();
}
}
}
void bfsTraverse() {
queue<node*>q;
q.push(this);
q.push(NULL);
bool leafFound = false;
while(!q.empty()) {
node* cur = q.front();
q.pop();
if(cur != NULL && cur->isLeaf) leafFound = true;
if(cur == NULL) {
cout << endl;
if(leafFound) break;
q.push(NULL);
continue;
}
cur->printVal();
for(int i=0; i<cur->szp; i++) q.push(cur->ptr[i]);
}
}
void printNode() {
cout << "DFS: " << endl;
cout << "PARPTR: " << parPtrID << endl;
cout << "Val: " << sz << " Ptr: " << szp << " isLeaf: " << isLeaf << endl;
cout << "Values: ";
for(int i=0; i<sz; i++) cout << val[i] << ' '; cout << endl;
getchar();
}
void printVal() {
for(int i=0; i<sz; i++) cout << val[i] << ' ';
cout << " || " ;
}
};
//node* tree = NULL;
int main() {
node* tree;
tree = tree->create();
instr();
int op;
while(scanf("%d", &op) && op) {
int x;
if(op == 1) {
cout << "WHAT DO YOU WANT TO INSERT: " ;
scanf("%d", &x);
// x = rand() % 1000;
// printf("%d\n", x);
// getchar();
tree->insert(x);
while(tree->par != NULL) {
tree = tree->par;
}
}
else if(op == 2) {
cout << "WHAT DO YOU WANT TO ERASE: ";
scanf("%d", &x);
tree->remove(x);
}
else if(op == 3) {
tree->dfsTraverse();
}
else if(op == 4) {
tree->bfsTraverse();
}
else if(op == 5) {
cout << "WHAT DO YOU WANT TO FIND: " ;
scanf("%d", &x);
tree->search(x);
}
instr();
}
}