-
Notifications
You must be signed in to change notification settings - Fork 1
/
btree.h
55 lines (40 loc) · 1.01 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
#ifndef btreeH
#define btreeH
#include <vector>
#include <memory>
#include <limits>
namespace btree {
// 2-5 tree
const size_t range_min = 2;
const size_t range_max = 5;
typedef int key_t;
class tree;
class node {
public:
node(node* parent_) : size(0), parent(parent_) {}
void insert(key_t key);
std::string str();
void check_invariants(); // debug
private:
static void split(node* old_node);
void add(key_t key, std::unique_ptr<node>& other);
bool is_leaf();
size_t find_pos(const key_t& key);
void place_key(const key_t& key, const size_t pos);
node* parent;
size_t size;
key_t keys[range_max];
std::unique_ptr<node> children[range_max + 1];
friend class tree; // tree::reroot needs access to parent, split.
};
class tree {
public:
tree() : root(std::unique_ptr<node>(new node(nullptr))) {}
void insert(key_t key);
std::string str();
private:
void reroot();
std::unique_ptr<node> root;
};
} // namespace rtree
#endif