forked from zqczgl/BPlus_Tress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BPlus_tree.cpp
234 lines (228 loc) · 7.29 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
#include "BPlus_tree.h"
BPlusTree::BPlusTree(){
Root = NULL;
m_DataHead = NULL;
}
bool BPlusTree::insert(int key, book* data){
// 是否已经存在
if (search(key))
{
return false;
}
// 找到可以插入的叶子结点,否则创建新的叶子结点
if(Root == NULL)
{
Root = new LeafNode();
m_DataHead = (LeafNode*)Root;
}
if (Root->getKeyNum() >= MAXNUM_KEY&&Root->getType()==LEAF) // 若根节点为叶节点且结点已满,分裂
{
InternalNode* newNode = new InternalNode(); //创建新的内部节点
newNode->setChild(0, Root);
Root->split(newNode, 0); // 叶子结点分裂
Root = newNode; //更新根节点指针
}
recursive_insert(Root, key, data);
if (Root->getKeyNum() > MAXNUM_KEY) // 若根节点为内部节点且结点已满,分裂
{
InternalNode* newNode = new InternalNode(); //创建新的内部节点
newNode->setChild(0, Root);
Root->split(newNode, 0); // 叶子结点分裂
Root = newNode; //更新根节点指针
}
return true;
}
void BPlusTree::recursive_insert(Node* parentNode, int key, book* data)
{
if (parentNode->getType()==LEAF) // 叶子结点,直接插入
{
((LeafNode*)parentNode)->insert(key, data);
}
else
{
// 找到子结点
int childIndex = parentNode->getKeyIndex(key);
Node* childNode = ((InternalNode*)parentNode)->getChild(childIndex);
recursive_insert(childNode, key, data);
if (childNode->getKeyNum()>MAXNUM_KEY) // 子结点已满,需进行分裂
{
childNode->split(parentNode, childIndex);
}
}
}
bool BPlusTree::search(int key)
{
return recursive_search(Root, key);
}
bool BPlusTree::recursive_search(Node *pNode, int key)const
{
if (pNode==NULL) //检测节点指针是否为空,或该节点是否为叶子节点
{
return false;
}
else
{
int keyIndex = pNode->getKeyIndex(key);
//int childIndex = pNode->getChildIndex(key, keyIndex); // 孩子结点指针索引
int childIndex =keyIndex;
if (keyIndex<pNode->getKeyNum() && key==pNode->getKeyValue(keyIndex))
{
return true;
}
else
{
if (pNode->getType()==LEAF) //检查该节点是否为叶子节点
{
return false;
}
else
{
return recursive_search(((InternalNode*)pNode)->getChild(childIndex), key);
}
}
}
}
book* BPlusTree::find(int key) {
return recursive_find(Root,key);
}
void BPlusTree::ascend()const
{
LeafNode* itr = m_DataHead;
while(itr!=NULL)
{
for (int i=0; i<itr->getKeyNum(); ++i)
{
cout<<*itr->getData(i)<<endl;
}
itr = itr->getRightSibling();
}
}
bool BPlusTree::remove(int key)
{
if (!search(key)) //不存在
{
return false;
}
if (Root->getKeyNum() == 1&&Root->getType() == LEAF)//就一个元素,清空
{
Root=NULL;
m_DataHead=NULL;
return true;
}
recursive_remove(Root, key);
if(Root->getKeyNum()==0){
Root=((InternalNode*)Root)->getChild(0);
}
return true;
}
void BPlusTree::recursive_remove(Node* parentNode, int key)
{
if (key==169){
key=169;
}
int keyIndex = parentNode->getKeyIndex(key);
//二分修正
int childIndex= parentNode->getChildIndex(key, keyIndex); // 孩子结点指针索引
if (parentNode->getType()==LEAF)// 找到目标叶子节点
{
parentNode->removeKey(keyIndex, childIndex); // 直接删除
// 如果键值在内部结点中存在,也要相应的替换内部结点
if (childIndex==0 && Root->getType() != LEAF && parentNode != m_DataHead)
{
changeKey(Root, key, parentNode->getKeyValue(0));
}
}
else // 内结点
{
Node *pChildNode = ((InternalNode*)parentNode)->getChild(childIndex); //包含key的子树根节点
recursive_remove(pChildNode, key);
if (pChildNode->getKeyNum()==MINNUM_KEY-2) // 包含关键字达到下限值,进行相关操作
{
Node *pLeft = childIndex > 0 ? ((InternalNode*)parentNode)->getChild(childIndex - 1) : NULL; //左兄弟节点
Node *pRight = childIndex < parentNode->getKeyNum() ? ((InternalNode*)parentNode)->getChild(childIndex + 1) : NULL;//右兄弟节点
// 先考虑从兄弟结点中借
if (pLeft!=NULL && pLeft->getKeyNum()>=MINNUM_KEY)// 左兄弟结点可借
{
pChildNode->borrowFrom(pLeft, parentNode, childIndex-1, LEFT);
}
else if (pRight!=NULL && pRight->getKeyNum()>=MINNUM_KEY)//右兄弟结点可借
{
pChildNode->borrowFrom(pRight, parentNode, childIndex, RIGHT);
}
//左右兄弟节点都不可借,考虑合并
else if (pLeft!=NULL) //与左兄弟合并
{
pLeft->mergeChild(parentNode, pChildNode, childIndex-1,LEFT);
}
else if (pRight!=NULL) //与右兄弟合并
{
pChildNode->mergeChild(parentNode, pRight, childIndex,RIGHT);
}
}
}
}
void BPlusTree::changeKey(Node *pNode, int oldKey, int newKey)
{
if (pNode!=NULL && pNode->getType()!=LEAF)
{
int keyIndex = pNode->getKeyIndex(oldKey);
if (keyIndex<pNode->getKeyNum() && oldKey==pNode->getKeyValue(keyIndex)) // 找到
{
pNode->setKeyValue(keyIndex, newKey);
}
else // 继续找
{
changeKey(((InternalNode*)pNode)->getChild(keyIndex), oldKey, newKey);
}
}
}
book* BPlusTree::recursive_find(Node *pNode, int key) {
int keyIndex = pNode->getKeyIndex(key);
int childIndex = pNode->getChildIndex(key, keyIndex); // 孩子结点指针索引
if (pNode->getType()==LEAF)
{
return ((LeafNode*)pNode)->getData(keyIndex);
}
else
{
return recursive_find(((InternalNode*)pNode)->getChild(childIndex), key);
}
}
void BPlusTree::show() {
if(Root==NULL){return;}
queue<Node*> q;
q.push(Root);
return recursive_show(q);
}
void BPlusTree::recursive_show(queue<Node *> q) {
if(q.empty())
return;
int size=q.size();
for (int i = 0; i < size; ++i) {
Node* pNode=q.front();
q.pop();
if (pNode->getType()==LEAF)
{ cout<<"(";
for(int i=0;i<pNode->getKeyNum();i++){
//if(i==0)cout<<"(";
cout<<pNode->getKeyValue(i)<<" ";
//if(i==pNode->getKeyNum()-1)cout<<") ";
}
cout<<") ";
} else{
cout<<"(";
for(int i=0;i<pNode->getKeyNum();i++){
//if(i==0)cout<<"(";
cout<<pNode->getKeyValue(i)<<" ";
//if(i==pNode->getKeyNum()-1)cout<<") ";
}
cout<<") ";
for (int j = 0; j <= pNode->getKeyNum(); ++j) {
q.push(((InternalNode*)pNode)->getChild(j));
}
cout<<" ";
}
}
cout<<endl;
return recursive_show(q);
}