-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBPlusTree.h
174 lines (157 loc) · 4.14 KB
/
BPlusTree.h
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
#ifndef BPLUSTREE_H
#define BPLUSTREE_H
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int n=3,MAX=n; //number of child;
struct BPTnode
{
int currentNode;
BPTnode *parent,*next;
string *word,*part_of_speech;
bool lf;
BPTnode **point;
} ;
BPTnode *root,*firstLeaf;
BPTnode* createNode()
{
BPTnode *m=new BPTnode();
m->point = new BPTnode *[n+1];
m->word=new string[n];
m->part_of_speech=new string[n];
m->currentNode = 0;
m->parent = NULL;
m->next = NULL;
m->lf = true;
return m;
}
BPTnode *findLeaf(BPTnode *tempRt,string word)
{
while(tempRt->lf==false)
{
int i;
for(i=0; i<tempRt->currentNode; i++)
if(word<tempRt->word[i])
break;
tempRt = tempRt->point[i];
}
return tempRt;
}
void insertValueAndPoint(BPTnode *parent,string value,BPTnode *right)
{
int i=parent->currentNode-1;
for(;i>=0; i--)
{
if(parent->word[i]<=value)
break;
else
{
parent->word[i+1] = parent->word[i];
parent->point[i+2] = parent->point[i+1];
}
}
parent->word[i+1] = value;
parent->point[i+2] = right;
parent->currentNode++;
}
void insertMiddle(BPTnode *parent,string value,BPTnode *left,BPTnode *right)
{
if(parent==NULL)
{
parent = createNode();
parent->word[0] = value;
parent->point[0] = left;
parent->point[1] = right;
parent->currentNode++;
parent->lf = false;
root = parent;
left->parent = parent;
right->parent = parent;
return;
}
right->parent = parent;
insertValueAndPoint(parent,value,right);
if(parent->currentNode==MAX)
{
BPTnode *splitNode = createNode();
splitNode->lf = false;
int j=0;
for(int i=parent->currentNode-(n-1)/2;i<MAX; i++)
{
splitNode->word[j] = parent->word[i];
if(j==0)
{
splitNode->point[0] = parent->point[i];
splitNode->point[0]->parent = splitNode;
}
splitNode->point[j+1] = parent->point[i+1];
splitNode->point[j+1]->parent = splitNode;
j++;
}
parent->currentNode-=(n-1)/2+1;
splitNode->currentNode = (n-1)/2;
insertMiddle(parent->parent,parent->word[parent->currentNode],parent,splitNode);
}
}
void insertLeaf(string word,string part_of_speech)
{
BPTnode *leaf = findLeaf(root,word);
int i= leaf->currentNode-1;
if(i>-1)
{
for(; i>=0; i--)
{
if(word<leaf->word[i])
{
leaf->word[i+1] = leaf->word[i];
leaf->part_of_speech[i+1] = leaf->part_of_speech[i];
}
else break;
}
}
leaf->word[i+1] = word;
leaf->part_of_speech[i+1] = part_of_speech;
leaf->currentNode++;
if(leaf->currentNode==MAX)
{
BPTnode *splitNode = createNode();
int j=0;
for(int i=leaf->currentNode-n/2;i<MAX; i++)
{
splitNode->word[j] = leaf->word[i];
splitNode->part_of_speech[j] = leaf->part_of_speech[i];
j++;
}
leaf->currentNode-=n/2;
splitNode->currentNode = n/2;
splitNode->next = leaf->next;
leaf->next = splitNode;
insertMiddle(leaf->parent,splitNode->word[0],leaf,splitNode);
}
}
string searchInTree(string searchEnglish)
{
root = createNode();
BPTnode *leaf;
int i=0;
string word,part_of_speech;
ifstream ifile;
ifile.open("postags.txt");
if(!ifile)
return "problem";
while(ifile>>word)
{
getline(ifile,part_of_speech);
insertLeaf(word,part_of_speech);
}
leaf= findLeaf(root,searchEnglish);
for(i=0; i<leaf->currentNode; i++)
if(searchEnglish==leaf->word[i])
break;
if(i==leaf->currentNode)
return "unknown";
ifile.close();
return leaf->part_of_speech[i];
}
#endif