-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtree.cpp
380 lines (338 loc) · 9.35 KB
/
btree.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
#include <vector>
#include <concepts>
#include <iostream>
#include <memory>
#include "exceptions.cpp"
using namespace std;
using namespace __gnu_cxx;
/**
*
* @tparam K generic type of keys held in node
* @tparam V generic type of values held in node
* @tparam k min-size of keys/values in node
*/
template <typename K , typename V, size_t k>
requires totally_ordered<K>
class BTreeNode {
typedef unique_ptr<BTreeNode> node_ptr;
/**
* array of pairs containing keys and values
*/
vector<pair<K, V>> entries;
/**
* array of pointers to each child node
*/
vector<node_ptr> children;
/**
* boolean specifying if node is leaf or not
*/
bool leaf;
public:
/**
* empty constructor setting leaf to true
*/
explicit BTreeNode(const bool isLeaf = true) : entries(), children(), leaf(isLeaf) {};
/**
*
* @return vector<pair<K, V>> pointer to the vector containing all entries for the node
*/
const vector<pair<K, V>>& getEntries() const {
return entries;
}
/**
*
* @return true if node is leaf false otherwise
*/
[[nodiscard]] bool isLeaf() const {
return leaf;
}
/**
*
* @param isLeaf bool to set leaf to
*/
void setLeaf(const bool isLeaf) {
leaf = isLeaf;
}
/**
*
* @return vector<node_ptr>& pointer to the vector containing all pointers to
* child-nodes
*/
const vector<node_ptr>& getChildren() const {
return children;
}
/**
*
* @return size_t size of node
*/
[[nodiscard]] size_t size() const {
return entries.size();
}
/**
*
* @return 2 * k
*/
static constexpr size_t maxSize() {
return 2 * k;
}
/**
*
* @return bool true if node is full (size == 2k)
*/
[[nodiscard]] bool isFull() const {
return size() == maxSize();
}
/**
*
* @return true if node is overflowing
*/
[[nodiscard]] bool isOverflowing() const {
return size() > maxSize();
}
/**
*
* @param key K key to compare to
* @return __gnu_cxx::__normal_iterator<const pair<K, V>*, vector<pair<K, V>>> iterator
* to first position where the given key is smaller than or equal to another key in entries in node
*/
//__gnu_cxx::__normal_iterator<const pair<K, V>*, vector<pair<K, V>>>
__normal_iterator<const pair<K, V>*, vector<pair<K, V>>> findIndex(const K& key) const {
return lower_bound(entries.begin(), entries.end(), key,
[](const pair<K, V>& entry, const K& key) {
return entry.first < key;
});
}
/**
*
* @param key K key to insert into entries of node
* @param value V value to insert into entries of node
*/
void insert(const K& key, const V& value) {
auto it = lower_bound(entries.begin(), entries.end(), key,
[](const pair<K, V>& entry, const K& key) {
return entry.first < key;
});
if (it != entries.end() && it->first == key) {
it->second = value;
} else {
entries.emplace(it, key, value);
}
}
/**
*
* @param entry pair of key and value to insert into btree as const reference
*/
void insert(const pair<K, V>& entry ) {
insert(entry.first, entry.second);
}
/**
*
* @param child unique_ptr<BTreeNode> ptr to child node to insert into
* children
*/
void addChild(node_ptr child) {
children.push_back(move(child));
}
/**
*
* @param it __gnu_cxx::__normal_iterator<const pair<K, V>*, vector<pair<K, V>>> iterator to first position where key is lower bound
* @return size_t index of child node where key should be inserted
*/
size_t findChildIndex(const __normal_iterator<const pair<K, V>*, vector<pair<K, V>>> it) const {
return distance(entries.begin(), it);
}
/**
*
* @param index size_t index of element to pop from entries
* @return pair of key and value of element at given index
*/
pair<K, V> pop_at(size_t index) {
if (index >= entries.size()) {
throw out_of_range("Index out of range in pop_at()");
}
pair<K, V> entry = entries[index];
entries.erase(entries.begin() + index);
return entry;
}
/**
*
* @param key
* @return
*/
bool contains(const K& key) {
auto it = findIndex(key);
return (it != entries.end() && it->first == key);
}
/**
*
* @param from vector from which to move entries
* @param to vector into which entries should be moved
* @param index starting index in from
*/
template <typename T>
void move_entries(vector<T>* from, vector<T>* to, size_t index) {
if (index >= from->size()) {
throw out_of_range("Start index out of range in move_entries()");
}
to->insert(to->end(),
std::make_move_iterator(from->begin() + index),
std::make_move_iterator(from->end()));
from->erase(from->begin() + index, from->end());
}
/**
*
* @param i size_t index of child to split
*/
void splitChild(size_t i) {
node_ptr child = move(children[i]);
children.erase(children.begin() + i);
if (!child->isOverflowing()) {
throw logic_error("Cannot split a non-full child node");
}
insert(child->pop_at(k));
node_ptr newNode = make_unique<BTreeNode>(child->isLeaf());
move_entries(&child->entries, &newNode->entries, k);
if (!child->isLeaf()) {
move_entries(&child->children, &newNode->children, k + 1);
}
children.insert(children.begin() + i, move(child));
children.insert(children.begin() + i + 1, move(newNode));
}
};
//--------------------------------------------
/**
*
* @tparam K generic type of keys held in node
* @tparam V generic type of values held in node
* @tparam k min-size of keys/values in node
*/
template <typename K , typename V, size_t k>
requires totally_ordered<K>
class BTree {
typedef unique_ptr<BTreeNode<K, V, k>> node_ptr;
/**
* root of BTree
*/
node_ptr root;
public:
BTree() : root(make_unique<BTreeNode<K, V, k>>()) {}
/**
*
* @param key const K& reference of key to lookup in tree
* @return true if key exists, false otherwise
*/
bool contains(const K& key) const {
return contains_helper(root.get(), key);
}
/**
*
* @param key const K& reference of key of value to get
* @return V value for given key in tree
*/
V get(const K& key) const {
return get_helper(root.get(), key);
}
/**
*
* @param key const K& reference of key to insert into tree
* @param value const V& reference of value to insert into tree with given key
*/
void insert(const K& key, const V& value) {
if (root->isLeaf()) {
insert_root(key, value);
} else {
insert_helper(root.get(), key, value);
}
}
/**
* prints tree inOrder
*/
void print() const {
printInOrder(root.get());
cout << endl;
}
private:
/**
*
* @param node BTreeNode<K, V, k>* pointer to current node
* @param key const K& reference of key to lookup
* @return true if node contains key
*/
bool contains_helper(const BTreeNode<K, V, k>* node, const K& key) const {
auto it = node->findIndex(key);
if (it != node->getEntries().end() && it->first == key) {
return true;
}
if (node->isLeaf()) {
return false;
}
size_t childIndex = node->findChildIndex(it);
return contains_helper(node->getChildren()[childIndex].get(), key);
}
/**
*
* @param node BTreeNode<K, V, k>* pointer to current node
* @param key const K& reference of key for value to get
* @return V value for given tree
* @throws key_not_in_tree if given key is not in tree
*/
V get_helper(const BTreeNode<K, V, k>* node, const K& key) const {
auto it = node->findIndex(key);
if (it != node->getEntries().end() && it->first == key) {
return it->second;
}
if (node->isLeaf()) {
throw key_not_in_tree(to_string(key) + " is not in tree");
}
size_t childIndex = node->findChildIndex(it);
return get_helper(node->getChildren()[childIndex].get(), key);
}
/**
*
* @param key const K& reference of key to insert
* @param value const V& reference of value to insert with given key
*/
void insert_root(const K& key, const V& value) {
//assert(root->isLeaf);
root->insert(key, value);
if (root->isOverflowing()) {
node_ptr newNode = make_unique<BTreeNode<K, V, k>>(false);
newNode->addChild(move(root));
root = move(newNode);
root->splitChild(0);
}
}
/**
*
* @param node BTreeNode<K, V, k>* pointer to current node
* @param key const K& reference of key to insert
* @param value const V& reference of value to insert with given key
*/
void insert_helper(BTreeNode<K, V, k>* node, const K& key, const V& value) {
if (node->isLeaf()) {
node->insert(key, value);
} else {
size_t childIndex = node->findChildIndex(node->findIndex(key));
BTreeNode<K, V, k>* child = node->getChildren()[childIndex].get();
insert_helper(child, key, value);
if (child->isOverflowing()) {
node->splitChild(childIndex);
}
}
}
/**
*
* @param node BTreeNode<K, V, k>* pointer to current node
*/
void printInOrder(const BTreeNode<K, V, k>* node) const {
for (size_t i = 0; i < node->size(); i++) {
if (!node->isLeaf()) {
printInOrder(node->getChildren()[i].get());
}
cout << node->getEntries()[i].first << ": " << node->getEntries()[i].second << endl;
}
if (!node->isLeaf()) {
printInOrder(node->getChildren().back().get());
}
}
};