-
Notifications
You must be signed in to change notification settings - Fork 0
/
BPlusTree.h
64 lines (51 loc) · 1.91 KB
/
BPlusTree.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
// BPlusTree.h
#ifndef BPLUSTREE_H
#define BPLUSTREE_H
#include <iostream>
#include <vector>
#include <memory>
#include <algorithm>
#include <string>
#include <fstream>
#include <optional>
// B+ tree order
const int ORDER = 4;
// BPlusTree class forward declaration
template <typename TKey, typename TValue>
class BPlusTree;
// BPlusTreeNode class
template <typename TKey, typename TValue>
class BPlusTreeNode {
public:
BPlusTreeNode(bool leaf = false);
bool isLeaf;
std::vector<TKey> keys;
std::vector<std::shared_ptr<BPlusTreeNode>> children; // for internal nodes
std::vector<TValue> values; // for leaf nodes
std::shared_ptr<BPlusTreeNode> next; // for leaf nodes
};
// BPlusTreeNode constructor
template <typename TKey, typename TValue>
BPlusTreeNode<TKey, TValue>::BPlusTreeNode(bool leaf)
: isLeaf(leaf), next(nullptr) {}
// BPlusTree class
template <typename TKey, typename TValue>
class BPlusTree {
public:
BPlusTree();
~BPlusTree() = default; // default destructor
void insert(const TKey& key, const TValue& value);
std::optional<TValue> search(const TKey& key) const;
bool remove(const TKey& key);
void display() const;
std::optional<TValue> findMin() const;
std::optional<TValue> findMax() const;
std::vector<TValue> rangeSearch(const TKey& lowerBound, const TKey& upperBound) const;
private:
std::shared_ptr<BPlusTreeNode<TKey, TValue>> root; // shared pointer to the root
void insertInternal(const TKey& key, std::shared_ptr<BPlusTreeNode<TKey, TValue>> node, std::shared_ptr<BPlusTreeNode<TKey, TValue>> child);
void splitLeaf(std::shared_ptr<BPlusTreeNode<TKey, TValue>> leaf);
std::shared_ptr<BPlusTreeNode<TKey, TValue>> getParent(std::shared_ptr<BPlusTreeNode<TKey, TValue>> current, std::shared_ptr<BPlusTreeNode<TKey, TValue>> child);
void splitInternal(std::shared_ptr<BPlusTreeNode<TKey, TValue>> internal);
};
#endif // BPLUSTREE_H