-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbptreee.cc
305 lines (277 loc) · 7.08 KB
/
bptreee.cc
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include "bptree.h"
#include <vector>
#include <sys/time.h>
struct timeval
cur_time(void)
{
struct timeval t;
gettimeofday(&t, NULL);
return t;
}
void
print_tree_core(NODE *n)
{
printf("[");
for (int i = 0; i < n->nkey; i++) {
if (!n->isLeaf) print_tree_core(n->chi[i]);
printf("%d", n->key[i]);
if (i != n->nkey-1 && n->isLeaf) putchar(' ');
}
if (!n->isLeaf) print_tree_core(n->chi[n->nkey]);
printf("]");
}
void
print_tree(NODE *node)
{
print_tree_core(node);
printf("\n"); fflush(stdout);
}
NODE *
find_leaf(NODE *node, int key)
{
int kid;
if (node->isLeaf) return node;
for (kid = 0; kid < node->nkey; kid++) {
if (key < node->key[kid]) break;
}
return find_leaf(node->chi[kid], key);
}
NODE *
alloc_leaf(NODE *parent)
{
NODE *node;
if (!(node = (NODE *)calloc(1, sizeof(NODE)))) ERR;
node->isLeaf = true;
node->parent = parent;
node->nkey = 0;
return node;
}
NODE *
insert_in_leaf(NODE *leaf, int key, DATA *data)
{
int i;
if (key < leaf->key[0]) {
for (i = leaf->nkey; i > 0; i--) {
leaf->chi[i+1] = leaf->chi[i] ;
leaf->key[i] = leaf->key[i-1] ;
}
leaf->key[0] = key;
leaf->chi[0] = (NODE *)data;
}
else {
for (i = 0; i < leaf->nkey; i++) {
if (key < leaf->key[i]) break;
}
for (int j = leaf->nkey; j > i; j--) {
leaf->chi[j+1] = leaf->chi[j] ;
leaf->key[j] = leaf->key[j-1] ;
}
leaf->key[i] = key;
leaf->chi[i] = (NODE *)data;
}
leaf->nkey += 1;
return leaf;
}
void
insert_in_parent(NODE *leaf, int key, NODE *new_leaf){
//Rootだったら新しく作る
NODE *parent;
if(Root == leaf){
parent = alloc_leaf(NULL);
parent->key[0] = key;
parent->chi[0] = leaf;
parent->chi[1] = new_leaf;
parent->nkey = 1;
parent->isLeaf = false;
leaf->parent = parent;
new_leaf->parent = parent;
Root = parent;
return;
}
//Rootじゃなかったらinsert_in_leaf関数と同じようなことする
parent = leaf->parent;
//まだ元のparentに枠が余ってた場合
if(parent->nkey < N){
int i;
if(key < parent->key[0]){
for(i = parent->nkey; i > 0; i--){
parent->chi[i+1] = parent->chi[i];
parent->key[i] = parent->key[i-1];
}
parent->key[0] = key;
parent->chi[0] = leaf;
parent->chi[1] = new_leaf;
}
else{
for(i = 0; i < parent->nkey; i++){
if(key < parent->key[i]) break;
}
for(int j = parent->nkey; j > i; j--){
parent->chi[j+1] = parent->chi[j];
parent->key[j] = parent->key[j-1];
}
leaf->key[i] = key;
leaf->chi[i+1] = new_leaf;
}
parent->nkey += 1;
return;
}
//もう枠がなかった場合
else{
TEMP temp;
int i;
for(i = 0; i < N-1; i++){
temp.key[i] = parent->key[i];
}
for(i = 0; i < N; i++){
temp.chi[i] = parent->chi[i];
}
temp.nkey = N-1;
temp.isLeaf = parent->isLeaf;
if(key < temp.key[0]){
for(i = temp.nkey-1; i > 0; i--){
temp.chi[i+1] = temp.chi[i];
temp.key[i] = temp.key[i-1];
}
temp.key[0] = key;
temp.chi[0] = leaf;
temp.chi[1] = new_leaf;
}
else{
for(i = 0; i < temp.nkey; i++){
if(key < temp.key[i]) break;
}
for(int j = temp.nkey-1; j > i; j--){
temp.chi[j+1] = temp.chi[j];
temp.key[j] = temp.key[j-1];
}
temp.key[i] = key;
}
temp.nkey++;
NODE *new_parent;
new_parent = alloc_leaf(parent->parent);
new_parent->chi[N-1] = parent->chi[N-1];
for(int i = 0; i < N-1; i++){
parent->key[i] = 0;
parent->chi[i] = NULL;
}
parent->chi[i+1] = NULL;
parent->nkey = 0;
for(int i = 0; i < N/2; i++){
parent->chi[i] = temp.chi[i];
parent->key[i] = temp.key[i];
parent->nkey++;
new_parent->chi[i] = temp.chi[i+(N/2)];
new_parent->key[i] = temp.key[i+(N/2)];
new_parent->nkey++;
}
NODE *key_leaf = parent->chi[0];
while(true){
if(key_leaf->isLeaf == true) break;
key_leaf = key_leaf->chi[0];
}
int parent_key = key_leaf->key[0];
insert_in_parent(parent, parent_key, new_parent);
}
}
void
insert(int key, DATA *data)
{
NODE *leaf, *new_leaf;
if (Root == NULL) {
leaf = alloc_leaf(NULL);
Root = leaf;
}else {
leaf = find_leaf(Root, key);
}
if (leaf->nkey < N-1) {
insert_in_leaf(leaf, key, data);
}else { // split
//TEMP型の構造体にリーフをコピー
TEMP temp;
int i;
for(i = 0; i < N-1; i++){
temp.key[i] = leaf->key[i];
temp.chi[i] = leaf->chi[i];
}
temp.chi[i+1] = temp.chi[i+1];
temp.nkey = leaf->nkey;
temp.isLeaf = leaf->isLeaf;
//あたらしいkeyをいれる
if(key < temp.key[0]){
for(i = temp.nkey-1; i > 0; i--){
temp.chi[i+1] = temp.chi[i];
temp.key[i] = temp.key[i-1];
}
if(temp.chi[0]->key[0] > data->key){
temp.chi[1] = temp.chi[0];
temp.chi[0] = (NODE *)data;
}
else{
temp.chi[1] = (NODE *)data;
}
temp.key[0] = key;
}
else{
for(i = 0; i < temp.nkey; i++){
if(key < temp.key[i]) break;
}
for(int j = temp.nkey-1; j > i; j--){
temp.chi[j+1] = temp.chi[j];
temp.key[j] = temp.key[j-1];
}
temp.key[i] = key;
temp.chi[i+1] = (NODE *)data;
}
temp.nkey++;
//2つにリーフを分ける
new_leaf = alloc_leaf(leaf->parent);
new_leaf->chi[N-1] = temp.chi[N-1];
leaf->chi[N-1] = new_leaf;
for(int i = 0; i < N-1; i++){
leaf->key[i] = 0;
leaf->chi[i] = NULL;
}
leaf->nkey = 0;
for(i = 0; i < N/2; i++){
insert_in_leaf(leaf, temp.key[i], (DATA *)temp.chi[i]);
insert_in_leaf(new_leaf, temp.key[i+N/2], (DATA *)temp.chi[i+N/2]);
}
temp.isLeaf = false;
temp.nkey = 0;
for(int i = 0; i < N; i++){
temp.chi[i] = NULL;
temp.key[i] = 0;
}
temp.chi[N] = NULL;
int parent_key = new_leaf->key[0];
insert_in_parent(leaf, parent_key, new_leaf);
}
}
void
init_root(void)
{
Root = NULL;
}
int
interactive()
{
int key;
std::cout << "Key: ";
std::cin >> key;
return key;
}
int
main(int argc, char *argv[])
{
struct timeval begin, end;
init_root();
printf("-----Insert-----\n");
begin = cur_time();
while (true) {
insert(interactive(), NULL);
print_tree(Root);
}
end = cur_time();
return 0;
}