-
Notifications
You must be signed in to change notification settings - Fork 0
/
b_tree.cpp
58 lines (47 loc) · 960 Bytes
/
b_tree.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
#include <iostream>
#include "b_tree.h"
#include "node.h"
BTree::BTree() : root(nullptr), order(0) { }
void BTree::set_order(int _order) {
order = _order;
}
void BTree::insert(int _key) {
if (root == nullptr) {
root = new Node(order, true);
root->data[0] = _key;
root->keys = 1;
}
else {
if (root->keys == 2 * order - 1) {
Node* s = new Node(order, false);
s->child[0] = root;
s->split_child(0, root);
int i = 0;
if (s->data[0] < _key)
i++;
s->child[i]->insert_non_full(_key);
root = s;
}
else {
root->insert_non_full(_key);
}
}
}
bool BTree::remove(int _key) {
if (root != nullptr && search(_key))
root->remove(_key);
else
return false;
return true;
}
void BTree::print() {
if (root != nullptr)
root->traverse();
}
void BTree::level(int _level) {
if (root != nullptr)
root->level(root, _level);
}
Node* BTree::search(int _key) {
return (root == nullptr) ? nullptr : root->search(_key);
}