This repository has been archived by the owner on Sep 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BPTree.cpp
500 lines (417 loc) · 18.2 KB
/
BPTree.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
#include <iostream>
#include <vector>
#include <math.h>
#include "storage.cpp"
#include "record.h"
using namespace std;
struct BPKeyPtr {
void* dataptr;
int key;
};
class LLNode {
public:
void* ptr;
LLNode* next;
};
class BPNode {
public:
BPKeyPtr* keyptr;
void* nextbpnode;
int curnumkeys;
bool isleaf;
bool isroot;
bool isllnode;
BPNode(int maxkeys){
keyptr = new BPKeyPtr[maxkeys];
for(int i=0; i<maxkeys; i++){
keyptr[i].dataptr = nullptr;
keyptr[i].key = 0;
}
nextbpnode = nullptr;
curnumkeys = 0;
isleaf = false;
isllnode = false;
};
};
class BPTree {
public:
Storage *data;
Storage *index;
BPNode* root;
void* rootstorageptr;
int maxkeyspernode;
int s;
BPTree(size_t blocksize, Storage *data, Storage *index){
size_t noderemainingsize = blocksize - sizeof(void*) - sizeof(int) - sizeof(bool);
maxkeyspernode = 0;
s = 0;
size_t usedsize = sizeof(BPKeyPtr);
while((usedsize + sizeof(BPKeyPtr)) <= noderemainingsize){
usedsize += sizeof(BPKeyPtr);
maxkeyspernode++;
}
maxkeyspernode = 3;
root = nullptr;
this->data = data;
this->index = index;
}
void inserttotree(int numvotes, void* recptr){
//If root ptr is null, means no tree yet
if(root == nullptr){
LLNode* llnode = nullptr;
llnode = new LLNode();
llnode->ptr = recptr;
llnode->next = nullptr;
//void* storedptr = nullptr;
//storedptr = index->writeToDisk(&llnode, sizeof(llnode));
BPNode* node = new BPNode(maxkeyspernode);
node->keyptr[node->curnumkeys].dataptr = llnode;
node->keyptr[node->curnumkeys].key = numvotes;
node->isleaf = true;
node->curnumkeys++;
//storedptr = nullptr;
//storedptr = index->writeToDisk(&node, sizeof(node));
//rootstorageptr = storedptr;
root = node;
cout << "root: ";
displaybpnode(root);
} else {
BPNode* cursor = root;
BPNode* parent;
int parentindex = 0;
void* parentstorageptr;
void* cursorstorageptr;
cout << "root: ";
displaybpnode(root);
//Keep searching till reach leaf
while(cursor->isleaf == false){
parent = cursor;
//parentstorageptr = rootstorageptr;
//Search from left to right of bpnode:
for(int i = 0; i < cursor->curnumkeys; i++){
//Go left if smaller than key
if(numvotes < cursor->keyptr[i].key){
cursor = (BPNode*) cursor->keyptr[i].dataptr;
parentindex = i;
break;
}
if(i == cursor->curnumkeys - 1){
if((i + 1) > maxkeyspernode){
cursor = (BPNode*) cursor->nextbpnode;
} else {
cursor = (BPNode*) cursor->keyptr[i + 1].dataptr;
}
parentindex = i;
break;
}
}
cout << "parent: ";
displaybpnode(parent);
cout << "child: ";
displaybpnode(cursor);
}
//Reach leaf, find space to put
if(cursor->curnumkeys < maxkeyspernode){
int i = 0;
while(i < cursor->curnumkeys && numvotes > cursor->keyptr[i].key){
i++;
}
if(cursor->keyptr[i].key == numvotes){
LLNode* curnode = new LLNode();
LLNode* prenode = (LLNode*) cursor->keyptr[i].dataptr;
curnode->ptr = recptr;
curnode->next = nullptr;
while(prenode->next != nullptr){
prenode = prenode->next;
}
prenode->next = curnode;
//curnode = (LLNode*) index->loadFromDisk(cursor[i].keyptr->dataptr, sizeof(curnode));
} else {
//Shift everything back
for(int j = cursor->curnumkeys; j > i; j--){
cursor->keyptr[j].key = cursor->keyptr[j-1].key;
cursor->keyptr[j].dataptr = cursor->keyptr[j-1].dataptr;
}
//Add the current key in i, add ll to handle duplicates.
LLNode* llnode = nullptr;
llnode = new LLNode();
llnode->ptr = recptr;
llnode->next = nullptr;
cursor->keyptr[i].dataptr = llnode;
cursor->keyptr[i].key = numvotes;
cursor->curnumkeys++;
//Inserting to the start of node, means need update the parent.
/*if(i == 0 && parent->isleaf == false){
parent->keyptr[parentindex].key = numvotes;
}*/
}
cout << "space to put: ";
displaybpnode(cursor);
} else {
//Overflow
BPNode* newbpnode = new BPNode(maxkeyspernode);
//New node is leaf
newbpnode->isleaf = true;
//Create temp list & copy contents of existing
BPKeyPtr* templist = new BPKeyPtr[maxkeyspernode + 1];
for(int i=0; i < maxkeyspernode; i++){
templist[i].key = cursor->keyptr[i].key;
templist[i].dataptr = cursor->keyptr[i].dataptr;
}
//Inserting the new node.
//Find new slot in the templist
int i = 0;
while(i < maxkeyspernode && numvotes > templist[i].key){
i++;
}
//dk
if(i < cursor->curnumkeys){
if(cursor->keyptr[i].key == numvotes){
cout << "Overflow duplicate : " << numvotes << endl;
return;
}
}
//Shift all back
for(int j = maxkeyspernode + 1; j > i; j--){
templist[j].key = templist[j - 1].key;
templist[j].dataptr = templist[j - 1].dataptr;
}
//Create ll to handle duplicates
LLNode* llnode = nullptr;
llnode = new LLNode();
llnode->ptr = recptr;
llnode->next = nullptr;
//Add current i
templist[i].key = numvotes;
templist[i].dataptr = llnode;
cout << "templist: ";
for(int i=0; i < maxkeyspernode + 1; i++){
cout << templist[i].dataptr << "|" << templist[i].key << "|";
}
cout << endl;
//Calc the number of keys
cursor->curnumkeys = ceil((maxkeyspernode + 1) / 2.0);
newbpnode->curnumkeys = floor((maxkeyspernode + 1) / 2.0);
//Set the next node to the new node
cursor->nextbpnode = newbpnode;
//check parent index before & after to attach
/*if(parent->keyptr[parentindex - 1].dataptr != nullptr){
BPNode* prevnode = parent->keyptr[parentindex - 1].dataptr;
prevnode->nextbpnode = cursor;
}*/
//Add back the items into bpnodes from templist
for(i = 0; i < cursor->curnumkeys; i++){
cursor->keyptr[i].key = templist[i].key;
cursor->keyptr[i].dataptr = templist[i].dataptr;
}
for(int j = 0; j < newbpnode->curnumkeys; i++, j++){
newbpnode->keyptr[j].key = templist[i].key;
newbpnode->keyptr[j].dataptr = templist[i].dataptr;
}
//Previously once full, copy all to temp list & split, need to erase the "suppose" blank space.
for(i = cursor->curnumkeys; i < maxkeyspernode; i++){
cursor->keyptr[i].key = 0;
cursor->keyptr[i].dataptr = nullptr;
}
cout << cursor << ": ";
for(int i=0; i < maxkeyspernode; i++){
cout << cursor->keyptr[i].dataptr << "|" << cursor->keyptr[i].key << "|";
}
cout << cursor->nextbpnode << " -> " << newbpnode << ": ";
for(int i=0; i < maxkeyspernode; i++){
cout << newbpnode->keyptr[i].dataptr << "|" << newbpnode->keyptr[i].key << "|";
}
cout << newbpnode->nextbpnode << endl;
cout << "cursor: ";
displaybpnode(cursor);
cout << "newbpnode: ";
displaybpnode(newbpnode);
//If cursor was root & overflow means splitted & need new root
if(cursor == root) {
BPNode* newroot = new BPNode(maxkeyspernode);
newroot->keyptr[0].dataptr = cursor;
newroot->keyptr[0].key = newbpnode->keyptr[0].key;
newroot->keyptr[1].dataptr = newbpnode;
newroot->isleaf = false;
newroot->curnumkeys = 1;
root = newroot;
} else {
//Need new parent in the middle of tree
insertinsert(numvotes, parent, newbpnode);
}
displaybpnode(parent);
}
}
}
void insertinsert(int numvotes, BPNode* parent, BPNode* child) {
//If parent got space
if(parent->curnumkeys < maxkeyspernode){
//Find location in parent to insert the key
int i = 0;
while(i < parent->curnumkeys && numvotes > parent->keyptr[i].key){
i++;
}
//Location found for key, shifting right to slot in key
for(int j = parent->curnumkeys; j > i; j--){
parent->keyptr[j].key = parent->keyptr[j - 1].key;
}
//Shift right to slot in for ptr, if last ptr, put in the nextbpnode
for(int j = parent->curnumkeys + 1; j > i + 1; j--){
if(j == maxkeyspernode + 1){
parent->nextbpnode = parent->keyptr[j - 1].dataptr;
continue;
}
parent->keyptr[j].dataptr = parent->keyptr[j - 1].dataptr;
}
//Update keys & pointers for new child
parent->keyptr[i].key = child->keyptr[0].key;
parent->keyptr[i + 1].dataptr = child;
parent->curnumkeys++;
} else {
//Parent no space
//Create new parent node
BPNode* newparent = new BPNode(maxkeyspernode);
//Create a temp list to store first
BPKeyPtr* templist = new BPKeyPtr[maxkeyspernode];
//Lastptr to act as the last ptr cox non leaf can have keys + 1 ptr, this is the +1
void* lastptr = nullptr;
//Copy all the keys & ptr
for(int i = 0; i < maxkeyspernode; i++){
templist[i].key = parent->keyptr[i].key;
templist[i].dataptr = parent->keyptr[i].dataptr;
}
//Search location to insert key & ptr
int i = 0;
while(i < maxkeyspernode && numvotes > templist[i].key){
i++;
}
//Shift right for all those after pos we want to slot in
for(int z = maxkeyspernode; z > i; z--){
templist[z].key = templist[z - 1].key;
}
//Shift right for ptr
for(int z = maxkeyspernode + 1; z > i + 1; z--){
if(z == maxkeyspernode + 1){
lastptr = templist[z - 1].dataptr;
continue;
}
templist[z].dataptr = templist[z - 1].dataptr;
}
//Insert in the new key & ptr
templist[i].key = numvotes;
templist[i + 1].dataptr = child;
//Mark the new parent as not leaf, although the default
newparent->isleaf = false;
//Calculate the number of keys we should have based on lect.
//parent now being the left parent, newparent is the right parent
parent->curnumkeys = ceil(maxkeyspernode / 2.0);
newparent->curnumkeys = floor(maxkeyspernode / 2.0);
//Put back the keys & ptr
bool putright = false;
int z = 0;
for(int i = 0; i < maxkeyspernode; i++){
if(putright == false){
parent->keyptr[z].key = templist[i].key;
parent->keyptr[z].dataptr = templist[i].dataptr;
} else {
newparent->keyptr[z].key = templist[i].key;
newparent->keyptr[z].dataptr = templist[i].dataptr;
if(i == maxkeyspernode - 1){
newparent->keyptr[z + 1].dataptr = lastptr;
}
}
if(z == parent->curnumkeys){
putright = true;
z = 0;
} else {
z++;
}
}
//Erased unused key & ptr space
for(int i = parent->curnumkeys; i < maxkeyspernode; i++){
parent->keyptr[i].key = 0;
}
for(int i = parent->curnumkeys + 1; i < maxkeyspernode; i++){
if(i == parent->curnumkeys + 1){
parent->nextbpnode = nullptr;
}
parent->keyptr[i].dataptr = nullptr;
}
if(parent == root){
BPNode* newroot = new BPNode(maxkeyspernode);
newroot->keyptr[0].dataptr = parent;
newroot->keyptr[0].key = parent->keyptr[parent->curnumkeys].key;
newroot->keyptr[1].dataptr = newparent;
newroot->isleaf = false;
newroot->curnumkeys = 1;
root = newroot;
} else {
//Find parent
insertinsert(templist[parent->curnumkeys].key, parent, newparent);
}
}
}
BPNode* findparent(BPNode* cursor, BPNode* child, int key){
if(cursor->isleaf){
return nullptr;
}
BPNode* parent = cursor;
while(cursor->isleaf == false){
for(int i = 0; i < cursor->curnumkeys + 1; i++){
if(cursor->keyptr[i].dataptr == child){
return parent;
}
}
for(int i = 0; i < cursor->curnumkeys; i++){
if(key < cursor->keyptr[i].key){
parent = (BPNode*) cursor->keyptr[i].dataptr;
}
}
}
return nullptr;
}
void displaybpnode(BPNode* curnode){
cout << curnode->isleaf << "-" << curnode << ": |";
for(int z = 0; z < maxkeyspernode; z++){
if(curnode->keyptr[z].dataptr == nullptr){
cout << "null|";
} else {
cout << curnode->keyptr[z].dataptr << "|";
}
if(curnode->keyptr[z].key == 0){
cout << "x|";
} else {
cout << curnode->keyptr[z].key << "|";
}
}
if(curnode->nextbpnode == nullptr){
cout << "null";
} else {
cout << curnode->nextbpnode;
}
cout << endl;
}
void displaytree(BPNode* curnode, int curlevel){
if(curnode != nullptr){
cout << " level " << curlevel << ": ";
for(int i = 0; i < curlevel; i++){
cout << " ";
}
}
displaybpnode(curnode);
if(curnode->isleaf != true){
for(int i = 0; i < curnode->curnumkeys + 1; i++){
displaytree((BPNode*) curnode->keyptr[i].dataptr, curlevel + 1);
}
}
}
//Work in progress
void linkleaf(BPNode* root){
BPNode* cursor = root;
if(cursor->isleaf != true){
for(int i = 0; i < cursor->curnumkeys + 1; i++){
linkleaf((BPNode*) cursor->keyptr[i].dataptr);
}
}
}
};