-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB-Tree.cpp
More file actions
407 lines (310 loc) · 9.5 KB
/
B-Tree.cpp
File metadata and controls
407 lines (310 loc) · 9.5 KB
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
// B-Tree.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
//
#include <iostream>
#include<fstream>
#include<string>
using namespace std;
class BNode {
public:
BNode** child;
long long int* key;
int size;
bool leaf;
};
class BTree {
public:
BNode* root;
unsigned minDegree;
BTree(int t) {
minDegree = t;
root = new BNode;
CreateNode(root);
root->leaf = true;
}
~BTree() {
deleteNode(root);
}
void CreateNode(BNode* x) {
x->size = 0;
x->key = new long long int[2 * minDegree - 1];
x->child = new BNode * [2 * minDegree];
}
void deleteNode(BNode* x) {
if (!x->leaf) {
for (int i = 0; i <= x->size; i++) {
deleteNode(x->child[i]);
}
}
delete[](x->child);
delete[](x->key);
delete(x);
}
int nodeInsert(BNode* x, long long int k) {
int i;
for (i = x->size; i > 0 && k < x->key[i - 1]; i--) {
x->key[i] = x->key[i - 1];
x->child[i + 1] = x->child[i];
}
x->child[i + 1] = x->child[i];
x->key[i] = k;
x->size++;
return i;
}
void splitChild(BNode* x, int i) {
BNode* toSplit = x->child[i];
BNode* newNode = new BNode;
CreateNode(newNode);
newNode->leaf = toSplit->leaf;
newNode->size = minDegree - 1;
for (int j = 0; j < minDegree - 1; j++) {
newNode->key[j] = toSplit->key[j + minDegree];
}
if (!toSplit->leaf) {
for (int j = 0; j < minDegree; j++) {
newNode->child[j] = toSplit->child[j + minDegree];
}
}
toSplit->size = minDegree - 1;
nodeInsert(x, toSplit->key[minDegree - 1]);
x->child[i + 1] = newNode;
}
void insert(long long int k) {
if (root->size == 2 * minDegree - 1) {
BNode* newRoot = new BNode;
CreateNode(newRoot);
newRoot->leaf = false;
newRoot->child[0] = root;
root = newRoot;
splitChild(newRoot, 0);
}
BNode* current_node = root;
while (!current_node->leaf) {
int index = current_node->size - 1;
while (index >= 0 && k <current_node->key[index]) {
index--;
}
index++;
if (current_node->child[index]->size == 2 * minDegree - 1) {
splitChild(current_node, index);
if (k > current_node->key[index]) {
index++;
}
}
current_node = current_node->child[index];
}
nodeInsert(current_node, k);
}
long long int searchKey(long long int k) {
pair<BNode*, int> pair_node = search(k);
if (pair_node.first == nullptr) {
//
}
return pair_node.first->key[pair_node.second];
}
pair<BNode*, int> search(long long int k) {
BNode* x = root;
while (true) {
int i = findIndex(x, k);
if (i < x->size && k == x->key[i]) {
return pair<BNode*, int>(x, i);
}
else if (x->leaf) {
return pair<BNode*, int>(nullptr, 0);
}
else {
x = x->child[i];
}
}
}
int findIndex(BNode* x, long long int k) {
int i = 0;
while (i < x->size && x->key[i] < k) {
i++;
}
return i;
}
long long int remove(long long int k) {
BNode* current_node = root;
while (true){
int i = findIndex(current_node, k);
if (i < current_node->size && current_node->key[i] == k) {
long long int toReturn = current_node->key[i];
if (current_node->leaf) {
nodeDelete(current_node, i);
}
else {
BNode* left_child = current_node->child[i];
BNode* right_child = current_node->child[i + 1];
if (left_child->size >= minDegree) {
while (!(left_child->leaf)) {
fixChildSize(left_child, left_child->size);
left_child = left_child->child[left_child->size];
}
current_node->key[i] = nodeDelete(left_child, left_child->size - 1);
}
else if (right_child->size >= minDegree) {
while (!(right_child->leaf)) {
fixChildSize(right_child, 0);
right_child = right_child->child[0];
}
current_node->key[i] = nodeDelete(right_child, 0);
}
else {
mergeChildren(current_node, i);
current_node = left_child;
continue;
}
}
return toReturn;
}
else {
char result = fixChildSize(current_node, i);
if (result == 2) {
current_node = root;
}
else {
current_node = current_node->child[findIndex(current_node, k)];
}
}
}
}
char mergeChildren(BNode* parent, int i) {
BNode* left_child = parent->child[i];
BNode* right_child = parent->child[i + 1];
left_child->key[left_child->size] = nodeDelete(parent, i);
int j = ++(left_child->size);
for (int k = 0; k < right_child->size; k++) {
left_child->key[j + k] = right_child->key[k];
left_child->child[j + k] = right_child->child[k];
}
left_child->size += right_child->size;
left_child->child[left_child->size] = right_child->child[right_child->size];
delete[] right_child->child;
delete[] right_child->key;
delete right_child;
if (parent->size == 0) {
root = left_child;
delete[] parent->child;
delete[] parent->key;
delete parent;
return 2;
}
return 1;
}
char fixChildSize(BNode* parent, int index) {
BNode* child = parent->child[index];
if (child->size < minDegree) {
if(index == 0 && index != parent->size && parent->child[index + 1]->size >= minDegree){
BNode* right_child = parent->child[index + 1];
nodeInsert(child, parent->key[index]);
child->child[child->size] = right_child->child[0];
right_child->child[0] = right_child->child[1];
parent->key[index] = nodeDelete(right_child, 0);
}
else if (index != parent->size && parent->child[index + 1]->size >= minDegree && (parent->child[index + 1]->size > parent->child[index -1]->size)) {
BNode* right_child = parent->child[index + 1];
nodeInsert(child, parent->key[index]);
child->child[child->size] = right_child->child[0];
right_child->child[0] = right_child->child[1];
parent->key[index] = nodeDelete(right_child, 0);
}
else if (index != 0 && parent->child[index - 1]->size >= minDegree) {
BNode* left_child = parent->child[index - 1];
for (int i = nodeInsert(child, parent->key[index - 1]); i >= 0; i--) {
//child->child[i] = child->child[i - 1];
child->child[i + 1] = child->child[i];
}
child->child[0] = left_child->child[left_child->size];
parent->key[index - 1] = nodeDelete(left_child, left_child->size - 1);
}
else if (index != 0) {
return mergeChildren(parent, index - 1);
}
else {
return mergeChildren(parent, index);
}
return 1;
}
return 0;
}
long long int nodeDelete(BNode* x, int index) {
long long int toReturn = x->key[index];
x->size--;
while (index < x->size) {
x->key[index] = x->key[index + 1];
x->child[index + 1] = x->child[index + 2];
index++;
}
return toReturn;
}
};
int main(/*int argc, char* argv[]*/)
{
/*
for (int i = 0; i < 15; i++) {
int a;
cin >> a;
Test.insert(a);
}
*/
/*
if (argc < 3) {
return 1;
}
ifstream inFile(argv[1]);
if (!inFile) {
return 2;
}
ofstream outFile(argv[2]);
if (!outFile) {
return 3;
}*/
ifstream inFile("test.txt");
ofstream outFile("result.txt");
int n, t;
inFile >> t >> n;
BTree Tree(t);
for (int i = 0; i < n; i++) {
string command;
long long int x;
inFile >> command;
switch (command[0])
{
case '+':
inFile >> x;
Tree.insert(x);
outFile << Tree.root->size;
for (int i = 0; i < Tree.root->size; i++)
outFile << ' ' << Tree.root->key[i];
outFile << '\n';
break;
case '-':
inFile >> x;
Tree.remove(x);
if (Tree.root == nullptr) cout << '0' << '\n';
else {
outFile << Tree.root->size;
for (int i = 0; i < Tree.root->size; i++)
outFile << ' ' << Tree.root->key[i];
outFile << '\n';
}
break;
case '?':
inFile >> x;
if (Tree.search(x).first) outFile << "true\n";
else outFile << "false\n";
}
}
inFile.close();
outFile.close();
return 0;
}
// Запуск программы: CTRL+F5 или меню "Отладка" > "Запуск без отладки"
// Отладка программы: F5 или меню "Отладка" > "Запустить отладку"
// Советы по началу работы
// 1. В окне обозревателя решений можно добавлять файлы и управлять ими.
// 2. В окне Team Explorer можно подключиться к системе управления версиями.
// 3. В окне "Выходные данные" можно просматривать выходные данные сборки и другие сообщения.
// 4. В окне "Список ошибок" можно просматривать ошибки.
// 5. Последовательно выберите пункты меню "Проект" > "Добавить новый элемент", чтобы создать файлы кода, или "Проект" > "Добавить существующий элемент", чтобы добавить в проект существующие файлы кода.
// 6. Чтобы снова открыть этот проект позже, выберите пункты меню "Файл" > "Открыть" > "Проект" и выберите SLN-файл.