-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbplus_tree.cpp
324 lines (282 loc) · 7.32 KB
/
bplus_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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include "bplus_tree.h"
#include "FileOption.h"
#include <iostream>
#include <vector>
#include <cstring>
#include <cstdio>
#include <ctime>
using namespace std;
IndexNode::IndexNode(int order){
//索引节点初始化
this -> order = order;
this -> keynum = 0;
this -> isLeaf = false;
this -> keys = new char*[order + 1];//+1是因为超出时进行分裂
this -> parent = NULL;
this -> children = new Node*[order + 1];
}
LeafNode::LeafNode(int order){
//叶子节点初始化
this -> order = order;
this -> keynum = 0;
this -> isLeaf = true;
this -> keys = new char*[order + 1];
this -> parent = NULL;
this -> values = new char*[order + 1];
this -> next = NULL;
}
BPlusTree::BPlusTree(int order){
//初始化树
this -> order = order;
this -> root = new LeafNode(order);
}
BPlusTree::~BPlusTree(){
//释放树的内存
if(this -> root != NULL){
delete this -> root;
}
}
Node* BPlusTree::search(char* s, char* key){//查找需要插入到的叶子节点
Node* t = this -> root;
while(!t -> isLeaf){//如果t就是叶子节点直接返回,否则继续向下
int flag = 0;
for(int i = 0;i < t -> keynum;i++){
if(strcmp(key, t -> keys[i]) < 0){
t = ((IndexNode*)t) -> children[i];
flag = 1;
break;
}
}
if(flag == 0){//key大于所有关键字,则修改最后一个关键字为当前关键字并进入最后一个关键字
t -> keys[t -> keynum - 1] = key;
t = ((IndexNode*)t) -> children[t -> keynum - 1];
}
}
return t;
}
void BPlusTree::insert(char* s, char* key){//插入到节点
Node* t = search(s, key);
int flag = 0;
//叶子节点进行插入
for(int i = 0;i < t -> keynum;i++){
if(strcmp(key, t -> keys[i]) < 0){
flag = 1;
for(int j = t -> keynum - 1;j >= i;j--){
t -> keys[j + 1] = t -> keys[j];
((LeafNode*)t) -> values[j + 1] = ((LeafNode*)t) -> values[j];
}
t -> keys[i] = key;
((LeafNode*)t) -> values[i] = s;
t -> keynum ++;
break;
}
else if(strcmp(key, t -> keys[i]) == 0){
return ;
}
}
if(flag == 0){
t -> keys[t -> keynum] = key;
((LeafNode*)t) -> values[t -> keynum] = s;
t -> keynum ++;
}
//如果叶子节点个数超过阶数
if(t -> keynum > order){
//叶子节点分裂
Node* tnew = new LeafNode(this -> order);
int idx = t -> keynum / 2;
for(int i = idx;i < t -> keynum;i++){
tnew -> keys[i - idx] = t -> keys[i];
((LeafNode*)tnew) -> values[i - idx] = ((LeafNode*)t) -> values[i];
}
//t[0, keynum / 2 - 1];
//tnew[keynum / 2, keynum]
tnew -> keynum = (t -> keynum + 1) / 2;
t -> keynum = t -> keynum / 2;
tnew -> parent = t -> parent;
((LeafNode*)tnew) -> next = ((LeafNode*)t) -> next;
((LeafNode*)t) -> next = tnew;
key = t -> keys[t-> keynum - 1];
//将当前的左边节点最后一个节点放入父节点
while(t -> parent != NULL){
Node* left = t;
Node* right = tnew;
t = t -> parent;
for(int i = 0;i < t -> keynum;i++){
if(strcmp(key, t -> keys[i]) < 0){
((IndexNode*)t) -> children[i] = tnew;
// tnew -> parent = t;
for(int j = t -> keynum - 1;j >= i;j--){
t -> keys[j + 1] = t -> keys[j];
((IndexNode*)t) -> children[j + 1] = ((IndexNode*)t) -> children[j];
}
t -> keys[i] = key;
((IndexNode*)t) -> children[i] = left;
t -> keynum ++;
break;
}
}
//父节点的分裂
if(t -> keynum > this -> order){
IndexNode* innew = new IndexNode(this -> order);
int idxnew = t -> keynum / 2;
for(int i = idxnew;i < t -> keynum;i++){
innew -> keys[i - idxnew] = t -> keys[i];
innew -> children[i - idxnew] = ((IndexNode*)t) -> children[i];
}
for(int i = idx;i < t -> keynum;i++){
((IndexNode*)t) -> children[i] -> parent = innew;
}
//t[0, keynum / 2]
//innew[(keynum + 1) / 2, keynum]
innew -> keynum = (t -> keynum + 1)/ 2;
t -> keynum = t -> keynum / 2;
innew -> parent = t -> parent;
key = t -> keys[t -> keynum - 1];
tnew = innew;
}
else{
//父节点不用分裂
tnew -> parent = t;
return ;
}
}
if(t -> parent == NULL){
t -> parent = new IndexNode(this -> order);
((IndexNode*)t -> parent) -> children[0] = t;
((IndexNode*)t -> parent) -> children[1] = tnew;
t -> parent -> keys[0] = t -> keys[t -> keynum - 1];
t -> parent -> keys[1] = tnew -> keys[tnew -> keynum - 1];
t -> parent -> keynum += 2;
tnew -> parent = t -> parent;
this -> root = t -> parent;
}
}
}
bool BPlusTree::find(char* s, char* key){
Node* t = this -> root;
while(!t -> isLeaf){//如果t就是叶子节点直接返回,否则继续向下
int flag = 0;
for(int i = 0;i < t -> keynum;i++){
if(strcmp(key, t -> keys[i]) < 0){
t = ((IndexNode*)t) -> children[i];
flag = 1;
break;
}
else if(strcmp(key, t -> keys[i]) == 0){
return true;
}
}
if(flag == 0){
return false;
}
}
for(int i = 0;i < t -> keynum;i++){
if(strcmp(((LeafNode*)t) -> keys[i], s) == 0){
return true;
}
}
return false;
}
void BPlusTree::printKeys(){
if(this -> root-> keynum == 0){
cout<<"No nodes now"<<endl;
return ;
}
vector<Node*> vec;
Node* p = this -> root;
vec.push_back(p);
int idx = 1;
// int cnt = 0;
while(vec.size() != 0){
int size = vec.size();
for(int i = 0;i < size;i++){
if(!vec[i]->isLeaf){
for(int j = 0;j < ((IndexNode*)vec[i]) -> keynum;j++){
vec.push_back(((IndexNode*)vec[i]) -> children[j]);
}
}
// cout<<"size "<<vec[i] -> keynum<<endl;
for(int j = 0;j < vec[i] -> keynum;j++){
cout<<vec[i] -> keys[j]<<" ";
}
cout<<"||";
}
vec.erase(vec.begin(), vec.begin() + size);
cout<<endl;
}
}
void BPlusTree::printValues(){
Node* p = this -> root;
while(!p->isLeaf){
p = ((IndexNode*)p) -> children[0];
}
int idx = 0 ;
while(p != NULL){
for(int i = 0;i < p -> keynum;i++){
cout<<((LeafNode*)p) -> keys[i]<<",";
idx++;
}
p = ((LeafNode*)p) -> next;
}
}
int main(){
//直接插入数据需要环境保证是UTF-8编码
//读取文件需要保证环境是GB2312编码
clock_t start, finish;
start = clock();
BPlusTree* tree = new BPlusTree(15);
FileReader* fr = new FileReader();
char* fileLoc = { "./dict.txt" };
bool openRe = fr->openFile(fileLoc,"r");
if (openRe == false) {
cout << "文件打开错误" << endl;
system("pause");
return 0;
}
char* data = new char[BUFFER_SIZE];
memset(data, 0x00, BUFFER_SIZE);
int idx = 0;
while (fr->getline(data))
{
int length = strlen(data);
if (length == 0) continue;
char* m = new char[length+1];
memcpy(m, data, length+1);
tree->insert(m, m);
}
delete fr;
FileReader* fr_target = new FileReader();
fileLoc = { "./string.txt" };
openRe = fr_target->openFile(fileLoc,"r");
if (!openRe) {
cout << "error happen when open target file" << endl;
system("pause");
return 0;
}
FileWriter* fw = new FileWriter();
fileLoc = { "./bupt_18_1.txt" };
openRe = fw->openFile(fileLoc, "w");
if (!openRe) {
cout << "error happen when open result file" << endl;
system("pause");
return 0;
}
memset(data, 0x00, BUFFER_SIZE);
int i = 0;
while (fr_target->getline(data))
{
int length = strlen(data);
if (length == 0) continue;
if (tree -> find(data, data)) {
fw->putline(data);
i++;
}
}
delete fr_target;
delete fw;
finish = clock();
printf("string_march: %d runtime: %d\n",i, (finish - start) );
system("pause");
_sleep(3000);
return 0;
}