-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtreeinsert.cpp
270 lines (247 loc) · 7.52 KB
/
btreeinsert.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
#include <iostream>
#include <vector>
using namespace std;
struct valContainer
{
union
{
int x;
int y;
char z;
};
int type;
bool operator>(const valContainer& rhs)
{
if (type == 0) return x > rhs.x;
else if (type == 1) return y > rhs.y;
else return z > rhs.z;
}
};
struct Node
{
vector<vector<valContainer> > vals; //vals[0] => x values; vals[1] => y values; vals[2] => z values
vector<Node*> children; //children vector to store child addresses
//Node* parent; //convenience pointer to use in operations like split
bool is_leaf; //to check if the node is a leaf
Node(bool);
};
Node::Node(bool l)
{
//push x, y, z vectors
vals.push_back(vector<valContainer>());
vals.push_back(vector<valContainer>());
vals.push_back(vector<valContainer>());
is_leaf = l;
}
struct BTree{
Node* root; //root pointer
int type; //type = 0 => x; type = 1 => y; type = 2 => z; criteria to use as a key
int degree; //degree to determine the max and min number of keys in a node
void insert(int, int, char); //main insertion function
void insert_nonfull(int, int, char, Node*); //helper insertion function
void split(int, Node*); //helper function to split children
void preorder(Node*); //prints the tree in preorder
void remove(valContainer); //main remove function
};
void BTree::insert(int x, int y, char z)
{
if(!root)
{
root = new Node(true);
valContainer xcon;
valContainer ycon;
valContainer zcon;
xcon.x = x;
ycon.y = y;
zcon.z = z;
xcon.type = 0;
ycon.type = 1;
zcon.type = 2;
root->vals[0].push_back(xcon);
root->vals[1].push_back(ycon);
root->vals[2].push_back(zcon);
}
else
{
if(root->vals[0].size() == (2*degree - 1))
{
Node* newNode = new Node(false);
newNode->children.push_back(root);
split(0, newNode);
int index = 0;
if(type == 0)
{
if(newNode->vals[0][0].x < x) index++;
} else if (type == 1)
{
if(newNode->vals[1][0].y < y) index++;
} else
{
if(newNode->vals[2][0].z < z) index++;
}
insert_nonfull(x, y, z, newNode->children[index]);
root = newNode;
}
else
{
insert_nonfull(x, y ,z, root);
}
}
}
void BTree::split(int index, Node* parent)
{
Node* right_child = new Node(parent->children[index]->is_leaf);
//copy half of the values to the new child
for (int i = 0; i < degree-1; i++)
{
right_child->vals[0].push_back(parent->children[index]->vals[0][degree + i]);
right_child->vals[1].push_back(parent->children[index]->vals[1][degree + i]);
right_child->vals[2].push_back(parent->children[index]->vals[2][degree + i]);
}
for (int i = 0; i < degree-1; i++)
{
parent->children[index]->vals[0].pop_back();
parent->children[index]->vals[1].pop_back();
parent->children[index]->vals[2].pop_back();
}
//if the left child isn't a leaf then copy half of its children to right child
if(!parent->children[index]->is_leaf)
{
for (int i = 0; i < degree; i++)
{
right_child->children.push_back(parent->children[index]->children[degree + i]);
}
for (int i = 0; i < degree; i++)
{
parent->children[index]->children.pop_back();
}
}
//insert the mid key to parent node
parent->vals[0].insert(parent->vals[0].begin() + index, parent->children[index]->vals[0][degree - 1]);
parent->vals[1].insert(parent->vals[1].begin() + index, parent->children[index]->vals[1][degree - 1]);
parent->vals[2].insert(parent->vals[2].begin() + index, parent->children[index]->vals[2][degree - 1]);
//delete the mid key from the child node
parent->children[index]->vals[0].pop_back();
parent->children[index]->vals[1].pop_back();
parent->children[index]->vals[2].pop_back();
//insert right child to correct position => after the mid key
parent->children.insert(parent->children.begin() + index + 1, right_child);
}
void BTree::insert_nonfull(int x, int y, char z, Node* node)
{
if (node->is_leaf)
{
valContainer xcon;
valContainer ycon;
valContainer zcon;
xcon.x = x;
ycon.y = y;
zcon.z = z;
xcon.type = 0;
ycon.type = 1;
zcon.type = 2;
int pos = 0;
if (type == 0)
{
while(pos < node->vals[0].size() && node->vals[0][pos].x < x)
{
pos++;
}
}
else if (type == 1)
{
while(pos < node->vals[1].size() && node->vals[1][pos].y < y)
{
pos++;
}
}
else
{
while(pos < node->vals[2].size() && node->vals[2][pos].z < z)
{
pos++;
}
}
node->vals[0].insert(node->vals[0].begin() + pos, xcon);
node->vals[1].insert(node->vals[1].begin() + pos, ycon);
node->vals[2].insert(node->vals[2].begin() + pos, zcon);
}
else
{
int pos = 0;
if (type == 0)
{
while(pos < node->vals[0].size() && node->vals[0][pos].x < x)
{
pos++;
}
}
else if (type == 1)
{
while(pos < node->vals[1].size() && node->vals[1][pos].y < y)
{
pos++;
}
}
else
{
while(pos < node->vals[2].size() && node->vals[2][pos].z < z)
{
pos++;
}
}
if(node->children[pos]->vals[0].size() == (2*degree -1))
{
split(pos, node);
if (type == 0)
{
if (node->vals[0][pos].x < x) pos++;
}
else if (type == 1)
{
if (node->vals[1][pos].y < y) pos++;
}
else
{
if (node->vals[2][pos].z < z) pos++;
}
}
insert_nonfull(x, y, z, node->children[pos]);
}
}
void BTree::preorder(Node* root)
{
if (root)
{
for(int i = 0; i < root->vals[0].size(); i++)
{
cout << "(" << root->vals[0][i].x << "," << root->vals[1][i].y << "," << root->vals[2][i].z << ")";
}
cout << "\n";
for (int i = 0; i < root->children.size(); i++)
{
preorder(root->children[i]);
}
}
}
int main(int argc, char const *argv[])
{
int n, deg, type;
char ctype;
cin >> n >> deg >> ctype;
if(ctype == 'x') type = 0;
else if (ctype == 'y') type = 1;
else type = 2;
BTree tree;
tree.root = NULL;
tree.degree = deg;
tree.type = type;
int x, y; char z;
for(int i = 0; i < n; i++)
{
cin >> x >> y >> z;
tree.insert(x, y, z);
}
tree.preorder(tree.root);
return 0;
}