-
Notifications
You must be signed in to change notification settings - Fork 0
/
BPTNode.cpp
405 lines (396 loc) · 10.7 KB
/
BPTNode.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
#include "BPTNode.hpp"
// Define constructor of BPTnode
BPTnode::BPTnode (int n)
{
order=n;
parent=NULL; // Initialize values of parent, previour and next node to be Null
prev=NULL;
next=NULL;
keys=new int[n]; // Initialize values of keys
keyNum=0;
}
// Destructor of BPTnode
BPTnode::~BPTnode()
{
delete[] keys;
}
//Returns if the node is a leaf
bool BPTnode::IsLeaf()
{
return isLeaf;
}
int BPTnode::GetKeyNum ()
{
return keyNum;
}
int *BPTnode::GetKeys()
{
return keys;
}
int BPTnode::GetKeyIndex(int key)
{
int lft=0;
int rht=keyNum-1;
int mid;
while (lft<rht) //return the mid while splitting
{
mid=(lft + rht)/2;
if (keys[mid]==key)
return mid;
else if (keys[mid]>key)
rht=mid - 1;
else
lft=mid + 1;
}
if (lft==rht)
{
if (keys[lft]>=key)
return lft;
else
return lft+1;
}
return lft;
}
void BPTnode::decKeyNum ()
{
keyNum--; //decrease key num
}
BPTnode *BPTnode::GetParent ()
{
return parent; //return parent of a node
}
BPTnode *BPTnode::GetNext()
{
return next; //return next sibling pointer of node
}
BPTnode *BPTnode::GetPrev ()
{
return prev; //return previous pointer of node
}
void BPTnode::SetParent(BPTnode *node)
{
parent=node;
}
void BPTnode::SetNext(BPTnode *node)
{
next=node;
}
void BPTnode::SetPrev(BPTnode *node)
{
prev=node;
}
LNode::LNode(int n):BPTnode(n) //constructor for class LNode
{
isLeaf=true;
values=new float[n];
}
LNode::~LNode () //destructor for class LNode
{
delete[] values;
}
void LNode::insert (int key, float value) //for inserting n a leaf node
{
int index=GetKeyIndex (key);
for (int i=keyNum; i>index; i--)
{
keys[i]=keys[i - 1];
values[i]=values[i - 1];
}
keys[index]=key;
values[index]=value;
keyNum++;
}
void LNode::Delete(int key) //for deleting from a leaf node
{
int index=GetKeyIndex(key);
if (keys[index]==key)
{
decKeyNum ();
for (; index <keyNum; index++)
{
keys[index]=keys[index + 1];
values[index]=values[index + 1];
}
}
else
{
cout<<key<<" Not found!"<<endl;
return;
}
if (keyNum < order/2)
{
if (next && next->GetKeyNum()>order/2)
{
next->decKeyNum();
keys[keyNum]=next->GetKeys()[0];
values[keyNum++] =next->getValues()[0];
for (int i=0; i<next->GetKeyNum (); i++)
{
next->GetKeys()[i]=next->GetKeys()[i + 1];
next->getValues ()[i]=next->getValues ()[i + 1];
}
BPTnode *lftParent=parent;
BPTnode *rightParent=next->GetParent();
while (lftParent!=rightParent)
{
lftParent=lftParent->GetParent();
rightParent=rightParent->GetParent();
}
int pindex=lftParent->GetKeyIndex(keys[keyNum -1]);
if (lftParent->GetKeys()[pindex]!=keys[keyNum - 1])
pindex--;
lftParent->GetKeys()[pindex]=next->GetKeys()[0];
}
else if(prev&&prev->GetKeyNum()>order/2)
{
prev->decKeyNum();
for (int i=keyNum; i>0; i--)
{
keys[i]=keys[i - 1];
values[i]=values[i - 1];
}
keys[0]=prev->GetKeys ()[prev->GetKeyNum ()];
values[0]=prev->getValues()[prev->GetKeyNum ()];
keyNum++;
BPTnode *lftParent=prev->GetParent ();
BPTnode *rightParent=parent;
while (lftParent!=rightParent)
{
lftParent=lftParent->GetParent ();
rightParent=rightParent->GetParent ();
}
int pindex=lftParent->GetKeyIndex(keys[0]);
lftParent->GetKeys()[pindex]=keys[0];
}
else if (next)
merge(next);
else if (prev)
prev->merge (this);
}
}
void LNode::merge(BPTnode* rtNode) //merge operation while deleting
{
int *rightKeys=rtNode->GetKeys();
float *rightValues=rtNode->getValues();
int down=rightKeys[0];
for (int i=0; i<rtNode->GetKeyNum(); i++)
{
keys[keyNum+i]=rightKeys[i];
values[keyNum+i]=rightValues[i];
}
keyNum+=rtNode->GetKeyNum();
next=rtNode->GetNext();
if (next)
next->SetPrev(this);
rtNode->GetParent()->Delete(down);
delete rtNode;
}
BPTnode *LNode::split(int &key) //split operation while inserting
{
// New rht node
LNode *rtNode=new LNode(order);
// The key to be moved into the parent
key=keys[keyNum/2];
// Copy every key and value after keyNum / 2 to rtNode
for(int i=keyNum/2; i<keyNum; i++)
{
rtNode->keys[i-keyNum/2]=keys[i];
rtNode->values[i-keyNum/2]=values[i];
}
rtNode->keyNum=keyNum-keyNum/2;
keyNum=keyNum/2;
return rtNode;
}
float *LNode::getValues()
{
return values;
}
// Define InNode
InNode::InNode(int n):BPTnode(n) //constructor of InNode
{
isLeaf=false;
children=new BPTnode*[n+1];
}
InNode::~InNode() //destructor of InNode
{
delete[] children;
}
// insert a new key and node into the current internal node
void InNode::insert(int key, BPTnode* rtChild)
{
int index=GetKeyIndex(key);
// move every key and child after index
for (int i=keyNum; i>index; i--)
{
keys[i]=keys[i - 1];
children[i+1]=children[i];
}
keys[index]=key;
children[index+1]=rtChild;
rtChild->SetNext(children[index]->GetNext ());
rtChild->SetPrev(children[index]);
children[index]->SetNext (rtChild);
if (rtChild->GetNext ())
rtChild->GetNext ()->SetPrev(rtChild);
keyNum++;
}
// Used only when creating new internal Node
void InNode::insert(int key, BPTnode* lftChild, BPTnode* rtChild)
{
keys[keyNum]=key;
rtChild->SetPrev(lftChild);
rtChild->SetNext(lftChild->GetNext());
lftChild->SetNext(rtChild);
if (rtChild->GetNext ())
rtChild->GetNext ()->SetPrev (rtChild);
children[keyNum++]=lftChild;
children[keyNum]=rtChild;
}
void InNode::Delete(int key)
{
int index=GetKeyIndex(key);
decKeyNum();
if (keys[index]>key)
{
for (; index < keyNum; index++)
{
keys[index]=keys[index + 1];
children[index]=children[index + 1];
}
children[index]=children[index + 1];
}
else
{
for (; index < keyNum; index++)
{
keys[index] = keys[index + 1];
children[index + 1] = children[index + 2];
}
}
if (keyNum<order/2)
{
if (next && next->GetKeyNum ()>order/2)
{
next->decKeyNum();
int up=next->GetKeys()[0];
BPTnode *borrowed=next->getChildren()[0];
borrowed->SetParent(this);
BPTnode *lftParent=parent;
BPTnode *rightParent=next->GetParent();
while (lftParent!= rightParent)
{
lftParent=lftParent->GetParent();
rightParent=rightParent->GetParent();
}
int pindex=lftParent->GetKeyIndex(up);
if (lftParent->GetKeys()[pindex]<up)
pindex--;
int down = lftParent->GetKeys ()[pindex];
lftParent->GetKeys ()[pindex]=up;
keys[keyNum++]=down;
children[keyNum]=borrowed;
for (int i=0; i<next->GetKeyNum(); i++)
{
next->GetKeys()[i]=next->GetKeys()[i + 1];
next->getChildren ()[i]=next->getChildren()[i + 1];
}
next->getChildren()[next->GetKeyNum()]=next->getChildren()[next->GetKeyNum()+1];
}
else if(prev && prev->GetKeyNum()>order/2)
{
prev->decKeyNum ();
int up = prev->GetKeys ()[prev->GetKeyNum ()];
BPTnode *borrowed = prev->getChildren ()[prev->GetKeyNum () + 1];
borrowed->SetParent (this);
BPTnode *lftParent = prev->GetParent ();
BPTnode *rightParent = parent;
while (lftParent != rightParent)
{
lftParent=lftParent->GetParent();
rightParent=rightParent->GetParent();
}
int pindex=lftParent->GetKeyIndex(up);
int down=lftParent->GetKeys()[pindex];
lftParent->GetKeys()[pindex]=up;
for (int i =keyNum; i>0; i--)
{
keys[i]=next->GetKeys()[i+1];
children[i+1]=children[i];
}
children[1]=children[0];
keys[0]=down;
children[0]=borrowed;
}
else if (next)
merge (next);
else if (prev)
prev->merge (this);
}
}
void InNode::merge(BPTnode* rtNode)
{
int *rightKeys=rtNode->GetKeys();
BPTnode **rtChildren=rtNode->getChildren();
BPTnode *rightParent=rtNode->GetParent();
BPTnode *lftParent=parent;
while (lftParent!=rightParent)
{
lftParent=lftParent->GetParent();
rightParent=rightParent->GetParent();
}
int pindex=lftParent->GetKeyIndex(rightKeys[0]);
if (pindex == lftParent->GetKeyNum () || lftParent->GetKeys ()[pindex] > rightKeys[0])
pindex--;
int down=lftParent->GetKeys()[pindex];
if (keyNum==0)
{
keys[keyNum]=down;
for (int i=0; i<=rtNode->GetKeyNum(); i++)
{
keys[keyNum+i+1]=rightKeys[i];
children[keyNum+i+1]=rtChildren[i];
children[keyNum+i+1]->SetParent(this);
}
}
else
{
keys[keyNum - 1]=down;
for (int i = 0; i<=rtNode->GetKeyNum(); i++)
{
keys[keyNum+i] =rightKeys[i];
children[keyNum+i+1]=rtChildren[i];
children[keyNum+i+1]->SetParent (this);
}
}
keyNum+=1+rtNode->GetKeyNum();
next=rtNode->GetNext();
if (next)
next->SetPrev (this);
rtNode->GetParent ()->Delete (down);
delete rtNode;
}
BPTnode *InNode::split(int &key)
{
// New rht node
InNode *rtNode=new InNode(order);
// The key to be moved into the the parent
key=keys[keyNum/2];
// Copy every key and child after keyNum / 2 to rtNode
for (int i=keyNum/2 +1; i<keyNum; i++)
{
rtNode->keys[i-keyNum/2-1]=keys[i];
rtNode->children[i-keyNum/2-1]=children[i];
}
rtNode->keyNum=keyNum-keyNum/2-1;
rtNode->children[rtNode->keyNum]=children[keyNum];
keyNum=keyNum/2;
for (int i=0; i<=rtNode->GetKeyNum(); i++)
{
rtNode->children[i]->SetParent(rtNode);
}
return rtNode;
}
BPTnode **InNode::getChildren()
{
return children;
}