-
Notifications
You must be signed in to change notification settings - Fork 0
/
btree.h
516 lines (448 loc) · 12.7 KB
/
btree.h
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
#include <iostream>
#include <memory>
#include <mutex>
#include <shared_mutex>
#include <sstream>
#include <vector>
namespace btree
{
template <typename T>
struct Node
{
// no of keys stored in the node
int n;
// list of `n` keys
std::vector<T> keys;
// whether the node is a leaf
bool is_leaf;
// Array of the BTree Node's children
std::vector<std::shared_ptr<Node>> children;
Node()
{
n = 0;
is_leaf = true;
}
};
template <typename T>
using NodePtr = std::shared_ptr<Node<T>>;
template <typename T>
struct SearchResult
{
NodePtr<T> node;
int height;
int index;
bool isEmpty() { return (node == nullptr); }
};
template <typename T>
class BTree
{
// The minimum_degree
int t;
// The root of the tree
NodePtr<T> root;
std::shared_mutex rw_lock;
bool isFull(NodePtr<T> node);
T min(NodePtr<T> node);
T max(NodePtr<T> node);
SearchResult<T> search(NodePtr<T> node, const T & element, int height);
std::string toString(NodePtr<T> node);
void insert(NodePtr<T> node, const T & element);
void splitChild(NodePtr<T> node, int index);
void splitRoot();
void del(NodePtr<T> node, const T & element);
void mergeChildren(NodePtr<T> node, int index);
int appropriateIndex(NodePtr<T> node, const T & element);
void delInternalNode(NodePtr<T> node, int i, const T & element);
void ensureChildCanAccomodate(NodePtr<T> node, int index);
public:
// TODO: Figure out a good default minimum degree
BTree() { t = 2; }
/**
* Initialize a BTree object
* @param minimum_degree The minimum no of child nodes a non-root node can have
*/
BTree(int minimum_degree)
{
if (minimum_degree < 2)
throw std::invalid_argument("minimum_degree should be >= 2");
t = minimum_degree;
}
/**
* Initialize a BTree object
* @param minimum_degree The minimum no of child nodes a non-root node can have
* @param seed_stream A vector of elements to initialize/seed the tree with
*/
BTree(int minimum_degree, std::vector<T> seed_stream) : BTree<T>(minimum_degree)
{
for (T i : seed_stream)
insert(i);
}
/**
* Search for an element in the BTree
* @param element The element to search for
* @return the search result
*/
SearchResult<T> search(const T & element);
/**
* Find the minimum element in the BTree
* @return the min element
*/
T min();
/**
* Find the maximum element in the BTree
* @return the max element
*/
T max();
/**
* Insert an element into the BTree
* @param element to be inserted
*/
void insert(const T & element);
/**
* Delete element from BTree if it exists
* @param element to be deleted
*/
void del(const T & element);
/** String representation of a BTree is like the follows:
* BTree(keys={5},children={[keys={3},children={}],[keys={8},children={}]})
* Tree for it:
* ------
* | 5 |
* ------
* / \
* ------ ------
* | 3 | | 8 |
* ------ ------
* Here, each array is a node.
* The child nodes are in the same format.
* @return the string representation
*/
std::string toString();
void clear()
{
root = nullptr;
}
};
template <typename T>
bool BTree<T>::isFull(NodePtr<T> node)
{
return node->n == 2 * t - 1;
}
template <typename T>
T BTree<T>::min()
{
std::shared_lock<std::shared_mutex> rlock(rw_lock);
if (!root)
throw std::length_error("BTree is empty");
return min(root);
}
template <typename T>
T BTree<T>::min(NodePtr<T> node)
{
if (node->is_leaf)
return node->keys[0];
else
return min(node->children[0]);
}
template <typename T>
T BTree<T>::max()
{
std::shared_lock<std::shared_mutex> rlock(rw_lock);
if (!root)
throw std::length_error("BTree is empty");
return max(root);
}
template <typename T>
T BTree<T>::max(NodePtr<T> node)
{
auto n = node->n;
if (node->is_leaf)
return node->keys[n - 1];
else
return max(node->children[n]);
}
template <typename T>
SearchResult<T> BTree<T>::search(const T & element)
{
std::shared_lock<std::shared_mutex> rlock(rw_lock);
if (!root)
return SearchResult<T>{};
return search(root, element, 0);
}
template <typename T>
SearchResult<T> BTree<T>::search(NodePtr<T> node, const T & element, int height)
{
int i = 0;
// Find appropriate index where the key is likely to be found
while (i < node->n && node->keys[i] < element)
i++;
if (i < node->n && node->keys[i] == element)
// TODO: Type deduction doesn't work here for some reason. Investigate
return SearchResult<T>{node, height, i};
else if (node->is_leaf)
return SearchResult<T>{};
else
return search(node->children[i], element, height + 1);
}
// TODO: Support a safe version of this method
// TODO: Support a move version of this method
template <typename T>
void BTree<T>::insert(const T & element)
{
if (!search(element).isEmpty())
throw std::invalid_argument("Element already exists");
std::unique_lock wlock {rw_lock};
if (!root)
root = std::make_unique<Node<T>>();
if (isFull(root))
splitRoot();
// insert non-root
insert(root, element);
}
template <typename T>
void BTree<T>::splitRoot()
{
NodePtr<T> new_root = std::make_shared<Node<T>>();
new_root->is_leaf = false;
new_root->children.insert(new_root->children.begin(), root);
root = new_root;
splitChild(root, 0);
}
template <typename T>
void BTree<T>::insert(NodePtr<T> node, const T & element)
{
if (node->is_leaf)
{
// The assumption here is that the leaf node will always be non-full
// => n < 2t -1
int i = appropriateIndex(node, element);
// Insert at the correct index
node->keys.insert(node->keys.begin() + i , element);
// Increment size
node->n += 1;
}
else
{
// Find the appropriate location
int i = appropriateIndex(node, element);
// Split child if node is full
if (isFull(node->children[i]))
{
splitChild(node, i);
// Increment index in case the key propogated up from the split
// is larger than the previous index
if (node->keys[i] < element)
i += 1;
}
// Insert in child node
insert(node->children[i], element);
}
}
template <typename T>
void BTree<T>::splitChild(NodePtr<T> node, int index)
{
NodePtr<T> new_child = std::make_shared<Node<T>>();
NodePtr<T> child = node->children[index];
new_child->is_leaf = child->is_leaf;
new_child->n = t - 1;
// Copy over keys at indices [t-1, 2t-1) to new node
new_child->keys.insert(new_child->keys.begin(), child->keys.begin() + t, child->keys.end());
// Remove excess keys from the node
child->keys.resize(t - 1);
if (!child->is_leaf)
{
// Copy over child nodes from indices [t, 2t)
new_child->children.insert(new_child->children.begin(), child->children.begin() + t, child->children.end());
// Remove the excess child pointers from the array
child->children.resize(t);
}
// Set size of child to t-1
child->n = t - 1;
// Move node's children pointers right one index
// The last index for children is n+1
// We want to move elements in (index, n+1] to the right
for (int i = node->n; i > index + 1; i--)
node->children.insert(node->children.begin() + i + 1, node->children[i]);
// Make the new child node the child of the parent
node->children.emplace(node->children.begin() + index + 1, new_child);
// Move node's keys to the right by one index
for (int i = node->n - 1; i >= index; i--)
node->keys.insert(node->keys.begin() + i + 1, node->keys[i]);
// move median key from child up into parent
node->keys.emplace(node->keys.begin() + index, child->keys[t - 1]);
// Increment size of keys in `node`
node->n += 1;
}
template <typename T>
std::string BTree<T>::toString()
{
std::ostringstream btree_repr;
btree_repr << "BTree(" << toString(root) << ")";
return btree_repr.str();
}
template <typename T>
std::string BTree<T>::toString(NodePtr<T> node)
{
if (node == nullptr)
return "";
std::ostringstream node_repr;
node_repr << "[keys={";
for (auto i : node->keys)
{
node_repr << i;
if (!(i == node->keys.back()))
node_repr << ",";
}
node_repr << "},children={";
for (auto c : node->children)
{
node_repr << toString(c);
if (!(c == node->children.back()))
node_repr << ",";
}
node_repr << "}]";
return node_repr.str();
}
template <typename T>
void BTree<T>::del(const T & element)
{
// We ascertain here that the tree contains the element
// so that we don't modify the tree unnecessarily
if (search(element).isEmpty())
return;
std::unique_lock<std::shared_mutex> wlock(rw_lock);
del(root, element);
}
template <typename T>
void BTree<T>::del(NodePtr<T> node, const T & element)
{
int i = appropriateIndex(node, element);
// If the element to delete is contained in the node
if (node->keys[i] == element)
{
if (node->is_leaf)
{
// The node is guaranteed to have at least t keys
// We can simply delete the key
node->keys.erase(node->keys.begin() + i);
node->n--;
}
else
delInternalNode(node, i, element);
}
else
{
if (node->children[i]->n < t)
ensureChildCanAccomodate(node, i);
del(node->children[i], element);
}
}
template <typename T>
int BTree<T>::appropriateIndex(NodePtr<T> node, const T & element)
{
int i = 0;
for (; i < node->n && node->keys[i] < element; i++)
;
return i;
}
template <typename T>
void BTree<T>::delInternalNode(NodePtr<T> node, int i, const T & element)
{
// If left child has >= t keys
// Delete predecessor from left child's subtree
// Replace the element with the predecessor
auto left_child = node->children[i];
if (left_child->n >= t)
{
auto replacement = max(left_child);
del(left_child, replacement);
node->keys[i] = replacement;
}
else
{
// If the right child has >= t keys
// Delete successor from right child's subtree
// Replace the element with the successor
auto right_child = node->children[i + 1];
if (right_child->n >= t)
{
auto replacement = min(right_child);
del(right_child, replacement);
node->keys[i] = replacement;
}
else
{
// Merge children and push the element to the merged node
mergeChildren(node, i);
// Delete the element from the merged node
del(left_child, element);
}
}
}
template <typename T>
void BTree<T>::mergeChildren(NodePtr<T> node, int index)
{
auto left_child = node->children[index];
auto right_child = node->children[index + 1];
left_child->n += right_child->n + 1;
// Move element to left_child
left_child->keys.push_back(node->keys[index]);
// Copy keys from right_child to left_child
left_child->keys.insert(left_child->keys.end(), right_child->keys.begin(), right_child->keys.end());
// Copy children pointers from right_child to left_child
left_child->children.insert(left_child->children.end(), right_child->children.begin(), right_child->children.end());
// Delete key and pointer from node
node->keys.erase(node->keys.begin() + index);
node->children.erase(node->children.begin() + index + 1);
node->n -= 1;
// Update root to the the merged child if node is root
if (root == node && node->n == 0)
root = left_child;
}
template <typename T>
void BTree<T>::ensureChildCanAccomodate(NodePtr<T> node, int index)
{
// Check if left sibling can donate
auto to_recurse = node->children[index];
if (index > 0 && node->children[index - 1]->n >= t)
{
auto left_sibling = node->children[index - 1];
// Move key from node to child
to_recurse->keys.insert(to_recurse->keys.begin(), node->keys[index - 1]);
// Move key from sibling to node
node->keys[index - 1] = left_sibling->keys.back();
left_sibling->keys.pop_back();
// Move child ptr from sibling to child
if (!to_recurse->is_leaf)
{
to_recurse->children.insert(to_recurse->children.begin(), left_sibling->children.back());
left_sibling->children.pop_back();
}
}
// Check if right sibling can donate
else if (index < node->n && node->children[index + 1]->n >= t)
{
auto right_sibling = node->children[index + 1];
// Move key from node to child
to_recurse->keys.push_back(node->keys[index]);
// Move key from sibling to node
node->keys[index] = right_sibling->keys.front();
right_sibling->keys.erase(right_sibling->keys.begin());
// Move child ptr from sibling to child
if (!to_recurse->is_leaf)
{
to_recurse->children.push_back(right_sibling->children.front());
right_sibling->children.erase(right_sibling->children.begin());
}
}
// Merge with sibling
else
{
if (index == node->n)
// Merge with left child
mergeChildren(node, index - 1);
else
mergeChildren(node, index);
}
}
}