-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtree.cpp
191 lines (146 loc) · 4.05 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
#include <iostream>
using namespace std;
// A BTree node
class BTreeNode {
public:
int* keys; // An array of keys
int t; // Minimum degree (defines the range for the number of keys)
BTreeNode** C; // An array of child pointers
int n; // Current number of keys
bool leaf; // Is true when node is leaf. Otherwise false
BTreeNode(int _t, bool _leaf); // Constructor
void traverse(); // Function to traverse all nodes in a subtree rooted with this node
BTreeNode* search(int k); // Function to search a key in the subtree rooted with this node
// A utility function to insert a new key in the subtree rooted with this node.
// The assumption is, the node must be non-full when this function is called
void insertNonFull(int k);
// A utility function to split the child y of this node. i is the index of y in child array C[].
// The child y must be full when this function is called
void splitChild(int i, BTreeNode* y);
friend class BTree;
};
class BTree {
BTreeNode* root; // Pointer to root node
int t; // Minimum degree
public:
BTree(int _t) {
root = nullptr;
t = _t;
}
// Function to traverse the tree
void traverse() {
if (root != nullptr)
root->traverse();
}
// Function to search a key in this tree
BTreeNode* search(int k) {
return (root == nullptr) ? nullptr : root->search(k);
}
// The main function that inserts a new key in this B-Tree
void insert(int k);
};
BTreeNode::BTreeNode(int _t, bool _leaf) {
t = _t;
leaf = _leaf;
keys = new int[2 * t - 1];
C = new BTreeNode * [2 * t];
n = 0;
}
void BTreeNode::traverse() {
int i;
for (i = 0; i < n; i++) {
if (leaf == false)
C[i]->traverse();
cout << " " << keys[i];
}
if (leaf == false)
C[i]->traverse();
}
BTreeNode* BTreeNode::search(int k) {
int i = 0;
while (i < n && k > keys[i])
i++;
if (keys[i] == k)
return this;
if (leaf == true)
return nullptr;
return C[i]->search(k);
}
void BTree::insert(int k) {
if (root == nullptr) {
root = new BTreeNode(t, true);
root->keys[0] = k;
root->n = 1;
}
else {
if (root->n == 2 * t - 1) {
BTreeNode* s = new BTreeNode(t, false);
s->C[0] = root;
s->splitChild(0, root);
int i = 0;
if (s->keys[0] < k)
i++;
s->C[i]->insertNonFull(k);
root = s;
}
else
root->insertNonFull(k);
}
}
void BTreeNode::insertNonFull(int k) {
int i = n - 1;
if (leaf == true) {
while (i >= 0 && keys[i] > k) {
keys[i + 1] = keys[i];
i--;
}
keys[i + 1] = k;
n = n + 1;
}
else {
while (i >= 0 && keys[i] > k)
i--;
if (C[i + 1]->n == 2 * t - 1) {
splitChild(i + 1, C[i + 1]);
if (keys[i + 1] < k)
i++;
}
C[i + 1]->insertNonFull(k);
}
}
void BTreeNode::splitChild(int i, BTreeNode* y) {
BTreeNode* z = new BTreeNode(y->t, y->leaf);
z->n = t - 1;
for (int j = 0; j < t - 1; j++)
z->keys[j] = y->keys[j + t];
if (y->leaf == false) {
for (int j = 0; j < t; j++)
z->C[j] = y->C[j + t];
}
y->n = t - 1;
for (int j = n; j >= i + 1; j--)
C[j + 1] = C[j];
C[i + 1] = z;
for (int j = n - 1; j >= i; j--)
keys[j + 1] = keys[j];
keys[i] = y->keys[t - 1];
n = n + 1;
}
int main() {
BTree t(3); // A B-Tree with minimum degree 3
t.insert(10);
t.insert(20);
t.insert(5);
t.insert(6);
t.insert(12);
t.insert(30);
t.insert(7);
t.insert(17);
cout << "Traversal of the constructed tree is ";
t.traverse();
int k = 6;
(t.search(k) != nullptr) ? cout << "\nPresent" : cout << "\nNot Present";
k = 15;
(t.search(k) != nullptr) ? cout << "\nPresent" : cout << "\nNot Present";
return 0;
}