-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbPlusTree.py
343 lines (270 loc) · 11.1 KB
/
bPlusTree.py
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import math
class Node:
def __init__(self, order=4, parent=None, keys=[], children=[]) -> None:
self.order = order
self.parent: Node = parent
self.keys = keys
self.children = children
def __str__(self) -> str:
return 'Node: keys =' + str(self.keys)
def __eq__(self, other):
return self.keys == other.keys
def isEmpty(self):
return len(self.keys) == 0
def isFull(self):
return len(self.keys) == self.order - 1
def isOverflow(self):
return len(self.keys) >= self.order
def isNearlyUnderflow(self):
return len(self.keys) <= math.ceil(self.order / 2)
def isUnderflow(self):
return len(self.keys) < math.ceil(self.order / 2)
def isRoot(self):
return self.parent is None
def isLeaf(self):
return isinstance(self, LeafNode)
def split(self): # 将一个满的结点分裂
leftNode = Node(self.order)
rightNode = Node(self.order)
leftNode.parent = self
rightNode.parent = self
splitConst = int(self.order // 2) # 分割常数(mid)
leftNode.keys = self.keys[ : splitConst]
leftNode.children = self.children[ : splitConst + 1]
rightNode.keys = self.keys[splitConst + 1 : ]
rightNode.children = self.children[splitConst + 1 : ]
self.children = [leftNode, rightNode]
self.keys = [self.keys[splitConst]]
for child in leftNode.children:
if isinstance(child, Node):
child.parent = leftNode
for child in rightNode.children:
if isinstance(child, Node):
child.parent = rightNode
return self # 返回分裂后的父亲
def findNextLevel(self, key):
for i, existedKey in enumerate(self.keys):
if key < existedKey:
return self.children[i], i
elif i + 1 == len(self.keys): # 最右边
return self.children[i + 1], i + 1
def findLeaf(self, key):
while not self.isLeaf():
self, _ = self.findNextLevel(key)
return self
def mergeUp(self, child, index):
# 插入过程中复用的函数。将child合并到self
self.children.pop(index)
pivot = child.keys[0]
for grandChild in child.children:
if isinstance(grandChild, Node):
grandChild.parent = self
for i, existedKey in enumerate(self.keys):
if pivot < existedKey:
self.keys = self.keys[ : i] + [pivot] + self.keys[i : ]
self.children = self.children[ : i] + child.children + self.children[i : ]
return
elif i + 1 == len(self.keys):
self.keys += [pivot]
self.children += child.children
return
def getPrevSibling(self): # 左边的兄弟
if self.isRoot() or not self.keys:
return None
_, index = self.parent.findNextLevel(self.keys[0])
if index >= 1: # 自己不是最左边的
return self.parent.children[index - 1]
else:
return None
def getNextSibling(self):
if self.isRoot() or not self.keys:
return None
_, index = self.parent.findNextLevel(self.keys[0])
if index + 1 < len(self.parent.children): # 自己不是最右边的
return self.parent.children[index + 1]
else:
return None
def borrowLeftNode(self, sibling, parentIndex):
parentKey = self.parent.keys.pop(-1)
siblingKey = sibling.keys.pop(-1)
child = sibling.children.pop(-1)
child.parent = self
self.parent.keys.insert(0, siblingKey)
self.keys.insert(0, parentKey)
self.children.insert(0, child)
def borrowRightNode(self, sibling, parentIndex):
parentKey = self.parent.keys.pop(0)
siblingKey = sibling.keys.pop(0)
child = sibling.children.pop(0)
child.parent = self
self.parent.keys.append(siblingKey)
self.keys.append(parentKey)
self.children.append(child)
def getLeftmostLeaf(self):
if not self:
return None
while not self.isLeaf():
self = self.children[0]
return self
def getRightmostLeaf(self):
if not self:
return None
while not self.isLeaf():
self = self.children[-1]
return self
class LeafNode(Node):
def __init__(self, order=4, parent=None, keys=[], children=[]) -> None:
super().__init__(order, parent, keys, children)
self.prevLeaf = None # 叶子链表的上一个
self.nextLeaf = None
# 特别注意:叶子结点的children是特殊的:它是value
def __str__(self) -> str:
return 'LeafNode: keys =' + str(self.keys)
def addKeyAndValue(self, key, value):
if not self.keys: # 结点没有key
self.keys.append(key) # 直接插入
self.children.append([value])
return
for i, existedKey in enumerate(self.keys): # 在适当的位置插入
# print('self.keys = ', self.keys)
if key == existedKey:
self.children[i].append(value)
return
elif key < existedKey:
self.keys = self.keys[ : i] + [key] + self.keys[i :]
self.children = self.children[ : i] + [[value]] + self.children[i : ]
return
elif i + 1 == len(self.keys):
self.keys.append(key)
self.children.append([value])
return
def split(self): # override
topNode = Node(self.order)
siblingNode = LeafNode(self.order)
splitConst = int(self.order // 2)
self.parent = topNode
siblingNode.parent = topNode
siblingNode.keys = self.keys[splitConst : ]
siblingNode.children = self.children[splitConst : ]
siblingNode.prevLeaf = self
siblingNode.nextLeaf = self.nextLeaf
topNode.keys = [siblingNode.keys[0]]
topNode.children = [self, siblingNode]
self.keys = self.keys[ : splitConst]
self.children = self.children[ : splitConst]
self.nextLeaf = siblingNode
return topNode
def borrowLeftNode(self, sibling, parentIndex): # override
key = sibling.keys.pop(-1)
data = sibling.children.pop(-1)
self.keys.insert(0, key)
self.children.insert(0, data)
self.parent.keys[parentIndex - 1] = key
def borrowRightNode(self, sibling, parentIndex):
key = sibling.keys.pop(0)
data = sibling.children.pop(0)
self.keys.append(key)
self.children.append(data)
self.parent.keys[parentIndex] = sibling.keys[0]
class BPlusTree:
INF = 0x3f3f3f3f
def __init__(self, order=4) -> None:
self.order = order
self.root = LeafNode(
order=order,
parent=None,
keys=[],
children=[]
) # 根结点
# 非常奇怪的一点:如果写LeafNode(order=order)会错!必须传所有参数
def find(self, key):
node = self.root
if node.isEmpty(): # 空树
return False, node, -1
node = node.findLeaf(key)
if node == self.root.getRightmostLeaf() and node.keys[-1] < key:
return False, node, BPlusTree.INF # 太大了
if node == self.root.getLeftmostLeaf() and node.keys[0] > key:
return False, node, -BPlusTree.INF # 太小了
for i, existedKey in enumerate(node.keys):
if existedKey == key:
return True, node, i
if existedKey > key:
return False, node, i
if i + 1 == len(node.keys):
return False, node, i + 1
def insert(self, key, value):
node = self.root
node = node.findLeaf(key)
node.addKeyAndValue(key, value) # node必为LeafNode
while node.isOverflow(): # 溢出了
if not node.isRoot(): # 不是根
parent = node.parent
node = node.split() # 分裂并设定为父亲
_, index = parent.findNextLevel(node.keys[0])
parent.mergeUp(node, index)
node = parent # 迭代向上检查
else:
node = node.split()
self.root = node
@staticmethod
def mergeOnDelete(leftNode: Node, rightNode: Node):
parent = leftNode.parent
_, index = parent.findNextLevel(leftNode.keys[0])
parentKey = parent.keys.pop(index)
parent.children.pop(index)
parent.children[index] = leftNode
if leftNode.isLeaf() and rightNode.isLeaf():
leftNode.nextLeaf = rightNode.nextLeaf
else:
leftNode.keys.append(parentKey)
for rightNodeChild in rightNode.children:
rightNodeChild.parent = leftNode
leftNode.keys += rightNode.keys
leftNode.children += rightNode.children
def delete(self, key):
node = self.root
node = node.findLeaf(key)
if key not in node.keys:
return False
index = node.keys.index(key)
node.children[index].pop() # 删
if len(node.children[index]) == 0: # 该结点删完了
node.children.pop(index) # 删掉 [] stub
node.keys.pop(index)
while node.isUnderflow() and not node.isRoot():
prevSibling = node.getPrevSibling()
nextSibling = node.getNextSibling()
_, parentIndex = node.parent.findNextLevel(key)
if prevSibling is not None and not prevSibling.isNearlyUnderflow(): # 尽可能借用
node.borrowLeftNode(prevSibling, parentIndex)
elif nextSibling is not None and not nextSibling.isNearlyUnderflow():
node.borrowRightNode(nextSibling, parentIndex)
elif prevSibling is not None and prevSibling.isNearlyUnderflow(): # 借不到再向上递归调整
self.mergeOnDelete(prevSibling, node)
elif nextSibling is not None and nextSibling.isNearlyUnderflow():
self.mergeOnDelete(node, nextSibling)
node = node.parent # 迭代向上
if node.isRoot() and not node.isLeaf() and len(node.children) == 1:
# 删除过程中出现了冗余根结点
self.root = node.children[0]
self.root.parent = None
return True
def getAllData(self): # 正序遍历B+树叶子链表。返回集合
data = set()
node = self.root.getLeftmostLeaf()
while node is not None:
for existedData in node.children:
data.add(existedData[0])
node = node.nextLeaf
return data
if __name__ == '__main__':
tree = BPlusTree(order=4)
n = 10
for i in range(n):
tree.insert(i * 4, i * 4)
# tp = tree.find(5)
# print(tp[0], tp[1], tp[2])
print(tree.getAllData())
newTree = BPlusTree(order=4)
print(newTree.getAllData())