-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBTree.cpp
414 lines (384 loc) · 11.4 KB
/
BTree.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
#include "BTree.h"
#include <iostream>
#include <math.h>
#include <climits>
Node::Node(bool isLeaf):keys(NULL),values(NULL),children(NULL),parent(NULL),isLeaf(isLeaf),occupancy(0),capacity(0){
if (isLeaf==true){
keys = new string[L+1];
values = new int[L+1];
for(int i = 0; i < L; i++){
values[i] = INT_MIN;
keys[i] = "";
}
capacity = L;
}
else{
keys = new string[M];
children = new Node*[M+1];
for (int i = 0; i < M; i++){
children[i]=NULL;
keys[i] = "";
}
children[M] = NULL;
capacity = M;
}
}
Node::Node(bool isLeaf, Node* parent):keys(NULL),values(NULL),children(NULL),parent(NULL),isLeaf(isLeaf),occupancy(0),capacity(0){
this->parent = parent;
if (isLeaf==true){
keys = new string[L+1];
values = new int[L+1];
for(int i = 0; i < L; i++){
values[i] = INT_MIN;
keys[i] = "";
}
capacity = L;
}
else{
keys = new string[M];
children = new Node*[M+1];
for (int i = 0; i < M; i++){
children[i]=NULL;
keys[i] = "";
}
children[M] = NULL;
capacity = M;
}
}
Node::~Node(){
delete keys;
delete values;
delete parent;
delete [] children;
}
// a function for LeafNode (data goes here)
// return the index of a given key in the keys array
// if name is NOT present in the array, return -1
int Node::IndexOfKey(string key) const{
int index = 0;
for (int i = 0; i < occupancy; i++){
if (key>=keys[i])
index++;
}
return index;
}
// returns a pointer to next level
// the pointer may point to either InternalNode or LeafNode
Node* Node::GetNextLevel(string key) const{
if(this->isLeaf)
return NULL;
return this->children[IndexOfChild(key)];
}
// A function for InternalNode (Road map)
// return the index of the pointer which lead us to next level
int Node::IndexOfChild(string key) const{
int index = 0;
while (index < occupancy-1 && key >= keys[index]){
//cout << "capacity"<<this->GetCapcity()<<endl;
index++;
}
//cout<<"index"<< index << endl;
return index;
}
// add node to noneleaf
Node* Node::Add(Node* child,Node* root){
// NoneLeafNode case
int index = this->IndexOfChild(child->keys[0]);
index ++;
for (int i = occupancy-1; i >= index;i--){
keys[i]=keys[i-1];
children[i+1] = children[i];
}
this->children[index]=child;
child->SetParent(this);
keys[index-1] = children[index]->GetKeyAt(0);
occupancy++;
// if the none leaf node is FULL
// need to split
if (occupancy>capacity){
if (parent == NULL){
return SplitRoot(root);
}
else
return SplitNoneLeaf(root);
}
return NULL;
}
// Add key-value to Leaf
Node* Node::Add(string key, int value, Node* root){
int index = this->IndexOfKey(key);
for (int i=this->occupancy-1;i>=index;i--){
//SetKeyAt(i+1, GetKeyAt(i));
keys[i+1]=keys[i];
values[i+1] = values[i];
}
SetKeyAt(index, key);
values[index]=value;
occupancy ++;
if (this->occupancy>this->capacity){
Node* temp = SplitLeaf(root);
if (temp)
return temp;
}
return NULL;
}
Node* Node::SplitRoot(Node* root){
Node* latterHalf = new Node(false);
int middle = ceil(static_cast<double>(M)/2);
int index = 0;
// for now, occupancy = 6 = capacity = M
// copy half of keys to new node
// key index 3,4 go to new node
for (int i = middle; i<occupancy-1; i++){
latterHalf->keys[index]=this->keys[i];
keys[i] = "";
//latterHalf->SetKeyAt(index, this->GetKeyAt(i));
index++;
}
index = 0;
// copy half of children to new node
// child index 3,4,5 go to new node
int origOccupancy = occupancy;
for (int i = middle; i<origOccupancy; i++){
latterHalf->children[index]=this->children[i];
latterHalf->children[index]->SetParent(latterHalf);
index++;
children[i]=NULL;
latterHalf->occupancy++;
this->occupancy--;
}
Node* newRoot = new Node(false);
Node* temp = latterHalf;
while (!temp->IsLeaf()){
temp = latterHalf->GetChildren()[0];
}
// temp is leaf now
newRoot->SetKeyAt(0,temp->GetKeyAt(0));
newRoot->SetChildrenAt(0, this);
this->SetParent(newRoot);
newRoot->SetChildrenAt(1, latterHalf);
latterHalf->SetParent(newRoot);
newRoot->IncrOccupancy();
newRoot->IncrOccupancy();
return newRoot;
}
Node* Node::SplitNoneLeaf(Node* root){
Node* latterHalf = new Node(false);
int middle = ceil(static_cast<double>(M)/2);
int index = 0;
// for now, occupancy = 6 = capacity = M
// copy half of keys to new node
// key index 3,4 go to new node
for (int i = middle; i<occupancy-1; i++){
latterHalf->keys[index]=this->keys[i];
keys[i] = "";
index++;
}
index = 0;
// copy half of children to new node
// child index 3,4,5 go to new node
int origOccupancy = occupancy;
for (int i = middle; i<origOccupancy; i++){
latterHalf->children[index]=this->children[i];
latterHalf->children[index]->SetParent(latterHalf);
index++;
children[i]=NULL;
latterHalf->occupancy++;
this->occupancy--;
}
Node* temp = this->GetParent()->Add(latterHalf,root);
if (temp!=NULL)
root = temp;
return root;
}
Node* Node::SplitLeaf(Node* root){
Node* newLeaf = new Node(true);
for (int i = L; i >= ceil(static_cast<double>(L)/2); i--){
// copy latterHalf(index 2,3) to new leaf
newLeaf->Add(keys[i], values[i],root);
keys[i] = "";
values[i]=INT_MIN;
this->occupancy--;
}
// this->next might be 0x646576697265442f
// this->previous might be 0x65646f63582f0009
// should not happen since we initialize all nodes' next and previous to NULL at the first place
if (this->next!=NULL/*&&this->next->keys[0]!=""*/)
this->next->SetPrevious(newLeaf);
newLeaf->next = this->next;
newLeaf->parent = this->parent;
newLeaf->previous = this;
this->next = newLeaf;
// insert new leaf to parent (which must be none-leaf)
// if this is not root
if(this->parent != NULL){
Node* temp = this->parent->Add(newLeaf,root);
if (temp!=NULL)
root = temp;
}
// current node is root
else{
Node* newRoot = new Node(false);
Node* temp = root;
root = newRoot;
newRoot->SetChildrenAt(0, temp);
temp->SetParent(root);
newRoot->IncrOccupancy();
root->Add(newLeaf, this);
}
return root;
// Add function automatically split parent if necessary
}
void Node::Print(){
if (IsLeaf()){
for (int i = 0; i < this->occupancy; i++){
cout << "Key: "<< this->keys[i] << " Value: " << this->values[i] << endl;
}
}
}
void BTree::Insert(string key, int value){
Node* parent = InsertHelper(key,root);
Node* leaf = parent->GetNextLevel(key);
int indexOfChild = parent->IndexOfChild(key);
if (parent->IsLeaf()){
count ++;
Node* tempRoot = parent->Add(key,value, root);
if (tempRoot!= NULL)
root = tempRoot;
}
else if (leaf == NULL){
count ++;
Node* newLeaf = new Node(true,parent);
newLeaf->SetKeyAt(0, key);
newLeaf->SetValueAt(0,value);
parent->GetChildren()[indexOfChild] = newLeaf;
// if newLeaf is the first leaf in that level
// its previous should be NULL
if (indexOfChild==0){
newLeaf->SetPrevious(NULL);
}
else{
newLeaf->SetPrevious(parent->GetChildren()[indexOfChild-1]);
newLeaf->GetPrevious()->SetNext(newLeaf);
}
newLeaf->SetNext(NULL);
newLeaf->IncrOccupancy();
parent->IncrOccupancy();
}
// leaf already exist, insert to leaf
// check if need to split leaf
else{
count ++;
Node* temp = leaf->Add(key,value,this->GetRoot());
if (temp)
this->root = temp;
}
}
// return a pointer points to the LAST level of the Road Map
// this level is right above the leaves
// thus we can construct pointers between leaves
// to make Range Qurey fast
Node* BTree::InsertHelper(string key, Node* current){
Node* result = current;
while(true){
if (count<=3)
break;
if (result->GetChildren()[0]->IsLeaf() == true)
break;/*
if (!result->IsLeaf()){
if (result->GetChildren()[0]){
result->GetChildren()[0]->IsLeaf();
break;
}
}*/
else
result = current->GetNextLevel(key);
}
return result;
}
// given a key and a node, search for the key
// return a pointer to a leaf node that might contains record
Node* BTree::SearchHelper(string key, Node* current) const{
if (current->IsLeaf())
return current;
return SearchHelper(key, current->GetNextLevel(key));
}
// calls search helper to look for key
bool BTree::Search(string key) const{
Node* result = SearchHelper(key,root);
// result must be a leaf node or NULL
if (result){
for (int i = 0; i < result->GetOccupancy(); i++){
if (result->GetKeyAt(i) == key){
cout << "Key: " << key << " Value: " << result->GetValueAt(i) << endl;
return true;
}
}
cout << "Not find!" << endl;
return false;
}
// if result == NULL
else{
cout << "Not find!" << endl;
return false;
}
}
/*void BTree::PrintAll(Node* root){
if (root == NULL)
return;
else if (root->IsLeaf()){
root->Print();
}
else{
for(int i = 0; i < root->GetOccupancy(); i++)
PrintAll(root->GetChildren()[i]);
}
}
*/
void BTree::PrintAll(Node* root){
if(root == NULL)
return;
if(root->IsLeaf()){
while(root->GetNext() != NULL){
root->Print();
root = root->GetNext();
}
root->Print();
}
else
PrintAll(root->GetChildren()[0]);
}
void BTree::PrintAllKeys(Node *root){
if(root == NULL)
return;
root->PrintAllKeys();
if(!root->IsLeaf())
for(int i = 0; i < M; i++)
PrintAllKeys(root->GetChildren()[i]);
}
void BTree::PrintBetween(string start, string end) {
Node* begin = SearchHelper(start, root);
Node* ending = SearchHelper(end, root);
//std::ostringstream oss;
///////// INDICATE NOTHING BETWEEN
if (begin!=NULL){
// print valid output in Node* start
for (int i = 0; i<begin->GetOccupancy(); i++){
if (begin->GetKeyAt(i)>=start)
cout<< "Key: " << begin->GetKeyAt(i) << " Value:" << begin->GetValueAt(i) << endl;
}
if(begin != ending){
Node* current = begin->GetNext();
// loop to print all data until Node* ending
while (current!=NULL && current!=ending) {
current->Print();
current=current->GetNext();
}
// print valid output in Node* ending
for (int i = 0; i<begin->GetOccupancy(); i++){
if (ending->GetKeyAt(i)<=end && ending->GetKeyAt(i)!="")
cout<<"Key: " << ending->GetKeyAt(i)<<" Value: "<< ending->GetValueAt(i) << endl;
}
}
}
}