forked from niteshkumartiwari/B-Plus-Tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove.cpp
336 lines (279 loc) · 10.1 KB
/
remove.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
#include <iostream>
#include <cstring>
#include "B+ Tree.h"
void BPTree::removeKey(int x) {
Node* root = getRoot();
// If tree is empty
if (root == NULL) {
cout << "B+ Tree is Empty" << endl;
return;
}
Node* cursor = root;
Node* parent;
int leftSibling, rightSibling;
// Going to the Leaf Node, Which may contain the *key*
// TO-DO : Use Binary Search to find the val
while (cursor->isLeaf != true) {
for (int i = 0; i < cursor->keys.size(); i++) {
parent = cursor;
leftSibling = i - 1;//left side of the parent node
rightSibling = i + 1;// right side of the parent node
if (x < cursor->keys[i]) {
cursor = cursor->ptr2TreeOrData.ptr2Tree[i];
break;
}
if (i == cursor->keys.size() - 1) {
leftSibling = i;
rightSibling = i + 2;// CHECK here , might need to make it negative
cursor = cursor->ptr2TreeOrData.ptr2Tree[i+1];
break;
}
}
}
// Check if the value exists in this leaf node
int pos = 0;
bool found = false;
for (pos = 0; pos < cursor->keys.size(); pos++) {
if (cursor->keys[pos] == x) {
found = true;
break;
}
}
auto itr = lower_bound(cursor->keys.begin(), cursor->keys.end(), x);
if (itr == cursor->keys.end()) {
cout << "Key Not Found in the Tree" << endl;
return;
}
// Delete the respective File and FILE*
string fileName = "DBFiles/" + to_string(x) + ".txt";
char filePtr[256];
strcpy(filePtr, fileName.c_str());
//delete cursor->ptr2TreeOrData.dataPtr[pos];//avoid memory leaks
if (remove(filePtr) == 0)
cout << "SuccessFully Deleted file: " << fileName << endl;
else
cout << "Unable to delete the file: " << fileName << endl;
// Shifting the keys and dataPtr for the leaf Node
for (int i = pos; i < cursor->keys.size()-1; i++) {
cursor->keys[i] = cursor->keys[i+1];
cursor->ptr2TreeOrData.dataPtr[i] = cursor->ptr2TreeOrData.dataPtr[i + 1];
}
int prev_size = cursor->keys.size();
cursor->keys.resize(prev_size - 1);
cursor->ptr2TreeOrData.dataPtr.resize(prev_size - 1);
// If it is leaf as well as the root node
if (cursor == root) {
if (cursor->keys.size() == 0) {
// Tree becomes empty
setRoot(NULL);
cout << "Ohh!! Our Tree is Empty Now :(" << endl;
}
}
cout << "Deleted " << x << " From Leaf Node successfully" << endl;
if (cursor->keys.size() >= (getMaxLeafNodeLimit()+1) / 2) {
//Sufficient Node available for invariant to hold
return;
}
cout << "UnderFlow in the leaf Node Happended" << endl;
cout << "Starting Redistribution..." << endl;
//1. Try to borrow a key from leftSibling
if (leftSibling >= 0) {
Node* leftNode = parent->ptr2TreeOrData.ptr2Tree[leftSibling];
//Check if LeftSibling has extra Key to transfer
if (leftNode->keys.size() >= (getMaxLeafNodeLimit()+1) / 2 +1) {
//Transfer the maximum key from the left Sibling
int maxIdx = leftNode->keys.size()-1;
cursor->keys.insert(cursor->keys.begin(), leftNode->keys[maxIdx]);
cursor->ptr2TreeOrData.dataPtr
.insert(cursor->ptr2TreeOrData.dataPtr.begin(), leftNode->ptr2TreeOrData.dataPtr[maxIdx]);
//resize the left Sibling Node After Tranfer
leftNode->keys.resize(maxIdx);
leftNode->ptr2TreeOrData.dataPtr.resize(maxIdx);
//Update Parent
parent->keys[leftSibling] = cursor->keys[0];
printf("Transferred from left sibling of leaf node\n");
return;
}
}
//2. Try to borrow a key from rightSibling
if (rightSibling < parent->ptr2TreeOrData.ptr2Tree.size()) {
Node* rightNode = parent->ptr2TreeOrData.ptr2Tree[rightSibling];
//Check if RightSibling has extra Key to transfer
if (rightNode->keys.size() >= (getMaxLeafNodeLimit() + 1) / 2 + 1) {
//Transfer the minimum key from the right Sibling
int minIdx = 0;
cursor->keys.push_back(rightNode->keys[minIdx]);
cursor->ptr2TreeOrData.dataPtr
.push_back(rightNode->ptr2TreeOrData.dataPtr[minIdx]);
//resize the right Sibling Node After Tranfer
rightNode->keys.erase(rightNode->keys.begin());
rightNode->ptr2TreeOrData.dataPtr.erase(rightNode->ptr2TreeOrData.dataPtr.begin());
//Update Parent
parent->keys[rightSibling-1] = rightNode->keys[0];
printf("Transferred from right sibling of leaf node\n");
return;
}
}
// Merge and Delete Node
if (leftSibling >= 0) {// If left sibling exists
Node* leftNode = parent->ptr2TreeOrData.ptr2Tree[leftSibling];
//Transfer Key and dataPtr to leftSibling and connect ptr2next
for (int i = 0; i < cursor->keys.size(); i++) {
leftNode->keys.push_back(cursor->keys[i]);
leftNode->ptr2TreeOrData.dataPtr
.push_back(cursor->ptr2TreeOrData.dataPtr[i]);
}
leftNode->ptr2next = cursor->ptr2next;
cout << "Merging two leaf Nodes" << endl;
removeInternal(parent->keys[leftSibling], parent, cursor);//delete parent Node Key
//delete cursor;
}
else if (rightSibling <= parent->keys.size()) {
Node* rightNode = parent->ptr2TreeOrData.ptr2Tree[rightSibling];
//Transfer Key and dataPtr to rightSibling and connect ptr2next
for (int i = 0; i < rightNode->keys.size(); i++) {
cursor->keys.push_back(rightNode->keys[i]);
cursor->ptr2TreeOrData.dataPtr
.push_back(rightNode->ptr2TreeOrData.dataPtr[i]);
}
cursor->ptr2next = rightNode->ptr2next;
cout << "Merging two leaf Nodes" << endl;
removeInternal(parent->keys[rightSibling-1], parent, rightNode);//delete parent Node Key
//delete rightNode;
}
}
void BPTree::removeInternal(int x, Node* cursor, Node* child) {
Node* root = getRoot();
// Check if key from root is to deleted
if (cursor == root) {
if (cursor->keys.size() == 1) {
// If only one key is left and matches with one of the
// child Pointers
if (cursor->ptr2TreeOrData.ptr2Tree[1] == child) {
setRoot(cursor->ptr2TreeOrData.ptr2Tree[0]);
//delete cursor;
cout << "Wow! New Changed Root" <<endl;
return;
}
else if (cursor->ptr2TreeOrData.ptr2Tree[0] == child) {
setRoot(cursor->ptr2TreeOrData.ptr2Tree[1]);
//delete cursor;
cout << "Wow! New Changed Root" << endl;
return;
}
}
}
// Deleting key x from the parent
int pos;
for (pos = 0; pos < cursor->keys.size(); pos++) {
if (cursor->keys[pos] == x) {
break;
}
}
for (int i = pos; i < cursor->keys.size()-1; i++) {
cursor->keys[i] = cursor->keys[i + 1];
}
cursor->keys.resize(cursor->keys.size() - 1);
// Now deleting the ptr2tree
for (pos = 0; pos < cursor->ptr2TreeOrData.ptr2Tree.size(); pos++) {
if (cursor->ptr2TreeOrData.ptr2Tree[pos] == child) {
break;
}
}
for (int i = pos; i < cursor->ptr2TreeOrData.ptr2Tree.size() - 1; i++) {
cursor->ptr2TreeOrData.ptr2Tree[i] = cursor->ptr2TreeOrData.ptr2Tree[i + 1];
}
cursor->ptr2TreeOrData.ptr2Tree
.resize(cursor->ptr2TreeOrData.ptr2Tree.size()-1);
// If there is No underflow. Phew!!
if (cursor->keys.size() >= (getMaxIntChildLimit() + 1) / 2 - 1) {
cout << "Deleted " << x << " from internal node successfully\n";
return;
}
cout << "UnderFlow in internal Node! What did you do :/" << endl;
if (cursor == root) {
return;
}
Node** p1 = findParent(root, cursor);
Node* parent = *p1;
int leftSibling, rightSibling;
// Finding Left and Right Siblings as we did earlier
for (pos = 0; pos < parent->ptr2TreeOrData.ptr2Tree.size(); pos++) {
if (parent->ptr2TreeOrData.ptr2Tree[pos] == cursor) {
leftSibling = pos - 1;
rightSibling = pos + 1;
break;
}
}
// If possible transfer to leftSibling
if (leftSibling >= 0) {
Node* leftNode = parent->ptr2TreeOrData.ptr2Tree[leftSibling];
//Check if LeftSibling has extra Key to transfer
if (leftNode->keys.size() >= (getMaxIntChildLimit() + 1) / 2 ) {
//transfer key from left sibling through parent
int maxIdxKey = leftNode->keys.size() - 1;
cursor->keys.insert(cursor->keys.begin(), parent->keys[leftSibling]);
parent->keys[leftSibling] = leftNode->keys[maxIdxKey];
int maxIdxPtr = leftNode->ptr2TreeOrData.ptr2Tree.size()-1;
cursor->ptr2TreeOrData.ptr2Tree
.insert(cursor->ptr2TreeOrData.ptr2Tree.begin(), leftNode->ptr2TreeOrData.ptr2Tree[maxIdxPtr]);
//resize the left Sibling Node After Tranfer
leftNode->keys.resize(maxIdxKey);
leftNode->ptr2TreeOrData.dataPtr.resize(maxIdxPtr);
return;
}
}
// If possible transfer to rightSibling
if (rightSibling < parent->ptr2TreeOrData.ptr2Tree.size()) {
Node* rightNode = parent->ptr2TreeOrData.ptr2Tree[rightSibling];
//Check if LeftSibling has extra Key to transfer
if (rightNode->keys.size() >= (getMaxIntChildLimit() + 1) / 2) {
//transfer key from right sibling through parent
int maxIdxKey = rightNode->keys.size() - 1;
cursor->keys.push_back(parent->keys[pos]);
parent->keys[pos] = rightNode->keys[0];
rightNode->keys.erase(rightNode->keys.begin());
//transfer the pointer from rightSibling to cursor
cursor->ptr2TreeOrData.ptr2Tree
.push_back(rightNode->ptr2TreeOrData.ptr2Tree[0]);
cursor->ptr2TreeOrData.ptr2Tree
.erase(cursor->ptr2TreeOrData.ptr2Tree.begin());
return;
}
}
//Start to Merge Now, if None of the above cases applied
if (leftSibling >= 0) {
//leftNode + parent key + cursor
Node* leftNode = parent->ptr2TreeOrData.ptr2Tree[leftSibling];
leftNode->keys.push_back(parent->keys[leftSibling]);
for (int val : cursor->keys) {
leftNode->keys.push_back(val);
}
for (int i = 0; i < cursor->ptr2TreeOrData.ptr2Tree.size(); i++) {
leftNode->ptr2TreeOrData.ptr2Tree
.push_back(cursor->ptr2TreeOrData.ptr2Tree[i]);
cursor->ptr2TreeOrData.ptr2Tree[i] = NULL;
}
cursor->ptr2TreeOrData.ptr2Tree.resize(0);
cursor->keys.resize(0);
removeInternal(parent->keys[leftSibling], parent, cursor);
cout << "Merged with left sibling"<<endl;
}
else if (rightSibling < parent->ptr2TreeOrData.ptr2Tree.size()) {
//cursor + parentkey +rightNode
Node* rightNode = parent->ptr2TreeOrData.ptr2Tree[rightSibling];
cursor->keys.push_back(parent->keys[rightSibling - 1]);
for (int val : rightNode->keys) {
cursor->keys.push_back(val);
}
for (int i = 0; i < rightNode->ptr2TreeOrData.ptr2Tree.size(); i++) {
cursor->ptr2TreeOrData.ptr2Tree
.push_back(rightNode->ptr2TreeOrData.ptr2Tree[i]);
rightNode->ptr2TreeOrData.ptr2Tree[i] = NULL;
}
rightNode->ptr2TreeOrData.ptr2Tree.resize(0);
rightNode->keys.resize(0);
removeInternal(parent->keys[rightSibling - 1], parent, rightNode);
cout << "Merged with right sibling\n";
}
}