-
Notifications
You must be signed in to change notification settings - Fork 3
/
z-btree.cpp
323 lines (266 loc) · 10.6 KB
/
z-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
#include <iostream>
#include <map>
#include <vector>
#include <fstream>
#include <iostream>
#include <chrono>
#include <algorithm> // For std::shuffle
#include <random> // For std::default_random_engine
#include <list>
#include <unordered_map>
#include <iostream>
#include <map>
#include <string>
#include <memory>
#include <sstream>
#include <limits>
#include <thread>
#include <queue>
#include <optional>
template <typename Key, typename Value>
class BPlusTree {
public:
struct Node {
std::vector<Key> keys;
std::vector<Value> values; // Only used in leaf nodes
std::vector<std::shared_ptr<Node>> children; // Only used in internal nodes
std::shared_ptr<Node> next = nullptr; // Next leaf node
bool isLeaf = false;
Node(bool leaf) : isLeaf(leaf) {}
};
int getValue(const Key& key) const {
auto node = root;
while (node != nullptr) {
if (node->isLeaf) {
auto it = std::lower_bound(node->keys.begin(), node->keys.end(), key);
if (it != node->keys.end() && *it == key) { // Key found in leaf
auto index = std::distance(node->keys.begin(), it);
return node->values[index];
}
break; // Key not found
} else {
auto it = std::upper_bound(node->keys.begin(), node->keys.end(), key);
node = (it == node->keys.begin()) ? node->children[0] :
node->children[std::distance(node->keys.begin(), it)];
}
}
return -1; // Indicate key not found
}
std::vector<Value> rangeQuery(const Key& lowerBound, const Key& upperBound) const {
std::vector<Value> result;
auto node = root;
// Traverse to the leaf node containing the lowerBound
while (node && !node->isLeaf) {
size_t i = 0;
while (i < node->keys.size() && lowerBound > node->keys[i]) {
++i;
}
node = node->children[i];
}
// Perform leaf-level traversal using `next`
while (node) {
for (size_t i = 0; i < node->keys.size(); ++i) {
if (node->keys[i] > upperBound) return result;
if (node->keys[i] >= lowerBound) {
std::cout << node->values[i] << " ";
result.push_back(node->values[i]);
}
}
node = node->next;
}
return result;
}
// Function to get a shared_ptr to the root node
std::shared_ptr<Node> getRoot() const {
return root;
}
void print() const {
printRecursive(root, 0);
std::cout << std::endl;
}
void printRecursive(std::shared_ptr<Node> node, int level) const {
if (!node) return;
// Indentation for readability, based on the level
std::string indent(level * 2, ' ');
std::cout << indent << "(L" << level << ") ";
// Print the node keys
for (const auto& key : node->keys) {
std::cout << key << " ";
}
std::cout << "\n";
// If it's not a leaf node, recursively print its children and their values
for (const auto& child : node->children) {
printRecursive(child, level + 1);
}
}
BPlusTree(size_t order) : maxKeys(order), root(std::make_shared<Node>(true)) {}
void insertOrUpdate(const Key& key, const Value& value) {
auto node = root;
std::vector<std::shared_ptr<Node>> path; // Track the path for backtracking
// Traverse to find the correct leaf node for the key
while (!node->isLeaf) {
path.push_back(node);
auto it = std::upper_bound(node->keys.begin(), node->keys.end(), key);
size_t index = it - node->keys.begin();
node = node->children[index];
}
// Attempt to find the key in the leaf node
auto it = std::lower_bound(node->keys.begin(), node->keys.end(), key);
if (it != node->keys.end() && *it == key) {
// Key found, update its value
size_t pos = std::distance(node->keys.begin(), it);
node->values[pos] += value;
std::cout << "Updating key " << key << " in node with first key " << node->keys.front() << std::endl;
} else {
// Key not found, insert new key-value pair
size_t pos = it - node->keys.begin();
std::cout << "Inserting key " << key << " into node with first key ";
if (node->keys.empty()) {
std::cout << "N/A (empty node)";
} else {
std::cout << node->keys.front();
}
std::cout << std::endl;
node->keys.insert(node->keys.begin() + pos, key);
node->values.insert(node->values.begin() + pos, value);
// Check for node overflow and split if necessary
if (node->keys.size() > maxKeys) {
splitNode(path, node);
}
}
}
private:
size_t maxKeys;
std::shared_ptr<Node> root;
void printNode(const std::shared_ptr<Node>& node) const {
if (!node) {
std::cout << "Empty Node";
return;
}
if (node->isLeaf) {
std::cout << "Leaf Node: ";
for (const auto& key : node->keys) std::cout << key << " ";
std::cout << "\n";
} else {
std::cout << "Internal Node: ";
for (const auto& key : node->keys) std::cout << key << " ";
std::cout << "\n";
}
}
void splitNode(std::vector<std::shared_ptr<Node>> path, std::shared_ptr<Node> node) {
auto newNode = std::make_shared<Node>(node->isLeaf);
size_t mid = node->keys.size() / 2;
Key midKey = node->keys[mid];
if (node->isLeaf) {
// Move second half of keys and values to the new node
std::move(node->keys.begin() + mid, node->keys.end(), std::back_inserter(newNode->keys));
std::move(node->values.begin() + mid, node->values.end(), std::back_inserter(newNode->values));
// Update next pointers to maintain the linked list of leaf nodes
newNode->next = node->next;
node->next = newNode;
// Trim original node's keys and values to reflect the split
node->keys.resize(mid);
node->values.resize(mid);
} else {
// For internal nodes, distribute keys and children to the new node
std::move(node->keys.begin() + mid + 1, node->keys.end(), std::back_inserter(newNode->keys));
std::move(node->children.begin() + mid + 1, node->children.end(), std::back_inserter(newNode->children));
// Trim original node's keys and children
node->keys.resize(mid);
node->children.resize(mid + 1);
}
// Update parent or create a new root if necessary
if (path.empty()) {
// Create a new root if necessary
auto newRoot = std::make_shared<Node>(false);
newRoot->keys.push_back(midKey);
newRoot->children.push_back(node);
newRoot->children.push_back(newNode);
root = newRoot;
} else {
auto parent = path.back();
path.pop_back(); // Remove the last element as we're going to handle it
// Position to insert the new child in the parent node
size_t pos = std::distance(parent->keys.begin(), std::lower_bound(parent->keys.begin(), parent->keys.end(), midKey));
parent->keys.insert(parent->keys.begin() + pos, midKey);
parent->children.insert(parent->children.begin() + pos + 1, newNode);
// Check if the parent node needs to be split
if (parent->keys.size() > maxKeys) {
splitNode(path, parent);
}
}
}
};
class OrderedIndex {
private:
BPlusTree<int, int> bptree;
public:
OrderedIndex() : bptree(3) {
}
void insertOrUpdate(int key, int value) {
bptree.insertOrUpdate(key, value);
}
int getValue(int key) const {
return bptree.getValue(key);
}
std::vector<int> rangeQuery(int lowerBound, int upperBound) const {
return bptree.rangeQuery(lowerBound, upperBound);
}
void print() const {
bptree.print();
}
};
int main() {
// Step 1: Initialize B+Tree
// Assuming the degree (minimum degree 't') of B+Tree is passed to its constructor
int t = 4; // Just an example, choose an appropriate value for your B+Tree
BPlusTree<int, int> tree(t);
int num_keys = 600;
// Generate a vector of keys
std::vector<int> keys(num_keys);
for (int i = 0; i < num_keys; ++i) {
keys[i] = i + 1; // Keys from 1 to 100
}
// Shuffle the keys to insert them in random order
std::random_device rd; // Non-deterministic random number generator
std::mt19937 g(rd()); // Standard mersenne_twister_engine seeded with rd()
std::shuffle(keys.begin(), keys.end(), g);
// Step 2: Insert elements into the tree
for (int key : keys) {
tree.insertOrUpdate(key, key);
}
// (Optional) Step 3: Verify the structure of the tree
// This step depends on having a method to print or otherwise inspect the structure of the tree
tree.print(); // Hypothetical method to visualize the tree structure
// (Optional) Step 4: Check if all inserted elements can be found
bool correct = true;
for (int i = 1; i <= num_keys; ++i) {
int value = tree.getValue(i); // Assuming find returns the value associated with the key, or a special value indicating not found
if (value != i) {
correct = false;
std::cout << "Error: Key " << i << " has incorrect value " << value << std::endl;
break;
}
}
if (correct) {
std::cout << "All elements inserted and found correctly." << std::endl;
} else {
std::cout << "There was an error in inserting or finding elements." << std::endl;
}
// Now, traverse the leaf nodes using the `next` pointer and print the keys
auto node = tree.getRoot(); // Assuming getRoot() provides access to the root node
// Traverse down to the first leaf
while (node && !node->isLeaf) {
node = node->children.front();
}
// Now traverse through the leaf nodes using the next pointer
std::cout << "Traversing leaf nodes: ";
while (node != nullptr) {
for (const auto& key : node->keys) {
std::cout << key << " ";
}
node = node->next;
}
std::cout << std::endl;
return 0;
}