-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBTreeNode.cpp
315 lines (251 loc) · 9.38 KB
/
BTreeNode.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
#include "BTreeNode.h"
BTreeNode::BTreeNode(int order, bool isLeaf) {
this->order = order;
this->isLeaf = isLeaf;
keysNum = 0;
// alokacja pamieci na maksymalna ilosc wartosci i dzieci
keys = new int[2 * order - 1];
children = new BTreeNode*[2 * order];
}
BTreeNode::~BTreeNode() {
if(!isLeaf && keysNum > 0)
for(int i = 0; i < keysNum + 1; i++)
delete children[i];
delete[] children;
delete[] keys;
}
void BTreeNode::SplitChild(int childIndex, BTreeNode* childToBeSplitted) {
BTreeNode* newChild = new BTreeNode(childToBeSplitted->order, childToBeSplitted->isLeaf);
newChild->keysNum = order - 1;
childToBeSplitted->keysNum = order - 1;
for(int i = 0; i < order - 1; i++)
newChild->keys[i] = childToBeSplitted->keys[i + order];
if(!newChild->isLeaf)
for(int i = 0; i < order; i++)
newChild->children[i] = childToBeSplitted->children[i + order];
// zrob miejsce i wstaw nowe dziecko do obecnego wezla
for(int i = keysNum; i > childIndex; i--)
children[i+1] = children[i];
children[childIndex + 1] = newChild;
// zrob miejsce i wstaw nowy klucz do obecnego wezla
for(int i = keysNum - 1; i >= childIndex; i--)
keys[i+1] = keys[i];
keys[childIndex] = childToBeSplitted->keys[order - 1];
keysNum++;
}
void BTreeNode::InsertToThisNotFullNode(int newKey) {
if(isLeaf) {
AddNewKey(newKey);
} else {
AddNewKeyToChild(newKey);
}
}
void BTreeNode::AddNewKey(int newKey) {
int i = keysNum - 1;
while(i >= 0 && keys[i] > newKey) {
keys[i+1] = keys[i];
i--;
}
keys[i+1] = newKey;
keysNum++;
}
void BTreeNode::AddNewKeyToChild(int newKey) {
int i = keysNum - 1;
while(i >= 0 && keys[i] > newKey)
i--;
int indexOfChildThatGetNewKey = i + 1;
if(children[i+1]->keysNum == (2 * order - 1)) {
SplitChild(i+1, children[i+1]);
if(keys[i+1] < newKey)
indexOfChildThatGetNewKey++;
}
children[indexOfChildThatGetNewKey]->InsertToThisNotFullNode(newKey);
}
void BTreeNode::Print() const {
int i;
for(i = 0; i < keysNum; i++) {
if(!isLeaf)
children[i]->Print();
std::cout << keys[i] << " ";
}
if(!isLeaf)
children[i]->Print();
}
BTreeNode* BTreeNode::Contains(const int key) {
int i = 0;
while(i < keysNum && keys[i] < key)
i++;
if(keys[i] == key && i < keysNum)
return this;
else if(!isLeaf)
return children[i]->Contains(key);
return nullptr;
}
void BTreeNode::Save() const {
int i;
std::cout << "( ";
for(i = 0; i < keysNum; i++) {
if(!isLeaf)
children[i]->Save();
std::cout << keys[i] << " ";
}
if(!isLeaf)
children[i]->Save();
std::cout << ") ";
}
void BTreeNode::LoadNode() {
// Klasa odpowiadajaca za wczytywaniu znaku za znakiem i przekonwertowaniu
// otrzymanego lancuchu na liczbe
StringToIntConverter stringInt;
bool previousCharWasNumber = false;
char input = getchar();
while(input != '\n' && input != ')') {
if(input == '(') {
isLeaf = false;
BTreeNode* newNode = new BTreeNode(order, true);
newNode->LoadNode();
children[keysNum] = newNode;
} else if((input >= '0' && input <= '9') || input == '-') {
stringInt.AddInt(input);
previousCharWasNumber = true;
} else if(input == ' ' && previousCharWasNumber) {
keys[keysNum++] = stringInt.GetInt();
previousCharWasNumber = false;
}
input = getchar();
}
}
void BTreeNode::Remove(const int keyToDelete) {
int removedKeyIndex = FindKeyIndex(keyToDelete);
if(removedKeyIndex != -1) {
if(isLeaf)
RemoveFromLeaf(removedKeyIndex);
else
RemoveFromInternalNode(removedKeyIndex);
return;
} else if(!isLeaf) {
int childIndexThatCanHaveKey = FindChildIndexThatCanHaveKey(keyToDelete);
bool isThisChildLast = (childIndexThatCanHaveKey == keysNum) ? true : false;
if(children[childIndexThatCanHaveKey]->keysNum == order - 1)
MakeChildNoMinKeysNode(childIndexThatCanHaveKey);
// jesli ostatnie dziecko zostalo polaczone z poprzednikiem
if(isThisChildLast && childIndexThatCanHaveKey > keysNum)
children[childIndexThatCanHaveKey - 1]->Remove(keyToDelete);
else
children[childIndexThatCanHaveKey]->Remove(keyToDelete);
}
}
void BTreeNode::RemoveFromLeaf(const int removedKeyIndex) {
for(int i = removedKeyIndex; i < keysNum - 1; i++)
keys[i] = keys[i+1];
keysNum--;
}
void BTreeNode::RemoveFromInternalNode(const int removedKeyIndex) {
BTreeNode* leftChild = children[removedKeyIndex];
BTreeNode* rightChild = children[removedKeyIndex + 1];
if(leftChild->keysNum > order - 1) {
int predecessor = GetPredecessor(removedKeyIndex);
keys[removedKeyIndex] = predecessor;
leftChild->Remove(predecessor);
} else if(rightChild->keysNum > order - 1) {
int successor = GetSuccessor(removedKeyIndex);
keys[removedKeyIndex] = successor;
rightChild->Remove(successor);
} else {
int keyToRemove = keys[removedKeyIndex];
MergeIntoSingleNode(removedKeyIndex, removedKeyIndex + 1);
children[removedKeyIndex]->Remove(keyToRemove);
}
}
int BTreeNode::GetPredecessor(const int keyIndex) {
BTreeNode* current = children[keyIndex];
while(!current->isLeaf)
current = current->children[current->keysNum];
return current->keys[keysNum - 1];
}
int BTreeNode::GetSuccessor(const int keyIndex) {
BTreeNode* current = children[keyIndex+1];
while(!current->isLeaf)
current = current->children[0];
return current->keys[0];
}
void BTreeNode::MakeChildNoMinKeysNode(int childIndex) {
if(childIndex != 0 && children[childIndex-1]->keysNum > order - 1)
BorrowLargestKeyFromSibling(children[childIndex], children[childIndex - 1]);
else if(childIndex != keysNum && children[childIndex + 1]->keysNum > order - 1)
BorrowSmallestKeyFromSibling(children[childIndex], children[childIndex + 1]);
else if(childIndex == keysNum)
MergeIntoSingleNode(childIndex - 1, childIndex);
else
MergeIntoSingleNode(childIndex, childIndex+1);
}
void BTreeNode::BorrowLargestKeyFromSibling(BTreeNode* childThatGetKey, BTreeNode* childThatGiveKey) {
int childThatGetKeyIndex = FindChildIndex(childThatGetKey);
for(int i = childThatGetKey->keysNum; i > 0; i--)
childThatGetKey->keys[i] = childThatGetKey->keys[i-1];
childThatGetKey->keys[0] = keys[childThatGetKeyIndex - 1];
if(!childThatGetKey->isLeaf)
for(int i = childThatGetKey->keysNum + 1; i > 0; i--)
childThatGetKey->children[i] = childThatGetKey->children[i-1];
if(!childThatGiveKey->isLeaf)
childThatGetKey->children[0] = childThatGiveKey->children[childThatGiveKey->keysNum];
keys[childThatGetKeyIndex - 1] = childThatGiveKey->keys[childThatGiveKey->keysNum - 1];
childThatGiveKey->keysNum--;
childThatGetKey->keysNum++;
}
void BTreeNode::BorrowSmallestKeyFromSibling(BTreeNode* childThatGetKey, BTreeNode* childThatGiveKey) {
int childThatGetKeyIndex = FindChildIndex(childThatGetKey);
childThatGetKey->keys[childThatGetKey->keysNum] = keys[childThatGetKeyIndex];
childThatGetKey->keysNum++;
if(!childThatGetKey->isLeaf)
childThatGetKey->children[keysNum] = childThatGiveKey->children[0];
keys[childThatGetKeyIndex] = childThatGiveKey->keys[0];
for(int i = 0; i < childThatGiveKey->keysNum - 1; i++)
childThatGiveKey->keys[i] = childThatGiveKey->keys[i+1];
if(!childThatGiveKey->isLeaf)
for(int i = 0; i < childThatGiveKey->keysNum; i++)
childThatGiveKey->children[i] = childThatGiveKey->children[i+1];
childThatGiveKey->keysNum--;
}
void BTreeNode::MergeIntoSingleNode(int firstNodeIndex, int secondNodeIndex) {
BTreeNode* firstNode = children[firstNodeIndex];
BTreeNode* secondNode = children[secondNodeIndex];
firstNode->keys[firstNode->keysNum++] = keys[firstNodeIndex];
for(int i = 0; i < secondNode->keysNum; i++)
firstNode->keys[firstNode->keysNum + i] = secondNode->keys[i];
if(!firstNode->isLeaf)
for(int i = 0; i <= secondNode->keysNum; i++)
firstNode->children[firstNode->keysNum + i] = secondNode->children[i];
// Przesuniecie kluczy i dzieci obecnego wezla
for (int i = firstNodeIndex; i < keysNum - 1; i++)
keys[i] = keys[i+1];
for (int i = secondNodeIndex; i < keysNum; i++)
children[i] = children[i+1];
keysNum--;
firstNode->keysNum += secondNode->keysNum;
secondNode->keysNum = 0;
delete secondNode;
}
int BTreeNode::FindKeyIndex(const int key) {
int index = -1;
for(int i = 0; i < keysNum; i++) {
if(keys[i] > key)
break;
else if(keys[i] == key)
index = i;
}
return index;
}
int BTreeNode::FindChildIndexThatCanHaveKey(const int key) {
int index = 0;
while (index < keysNum && keys[index] < key)
++index;
return index;
}
int BTreeNode::FindChildIndex(BTreeNode* child) {
int i = 0;
int childLastKey = child->keys[child->keysNum - 1];
while(keys[i] < childLastKey && i < keysNum)
i++;
return i;
}