-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathB-Plus-Tree.py
377 lines (288 loc) · 12 KB
/
B-Plus-Tree.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
from __future__ import annotations
from math import floor
from random import randint
class Node:
"""
Base node object.
Attributes:
order (int): The maximum number of keys each node can hold (branching factor).
"""
uidCounter = 0
def __init__(self, order):
self.order = order
self.parent: Node = None
self.keys = []
self.values = []
# This is for Debugging purposes only!
Node.uidCounter += 1
self.uid = self.uidCounter
def split(self) -> Node: # Split a full Node to two new ones.
left = Node(self.order)
right = Node(self.order)
mid = int(self.order // 2)
left.parent = right.parent = self
left.keys = self.keys[:mid]
left.values = self.values[:mid + 1]
right.keys = self.keys[mid + 1:]
right.values = self.values[mid + 1:]
self.values = [left, right] # Setup the pointers to child nodes.
self.keys = [self.keys[mid]] # Hold the first element from the right subtree.
# Setup correct parent for each child node.
for child in left.values:
if isinstance(child, Node):
child.parent = left
for child in right.values:
if isinstance(child, Node):
child.parent = right
return self # Return the 'top node'
def getSize(self) -> int:
return len(self.keys)
def isEmpty(self) -> bool:
return len(self.keys) == 0
def isFull(self) -> bool:
return len(self.keys) == self.order - 1
def isNearlyUnderflow(self) -> bool: # Used to check on keys, not data!
return len(self.keys) <= floor(self.order / 2)
def isUnderflow(self) -> bool: # Used to check on keys, not data!
return len(self.keys) <= floor(self.order / 2) - 1
def isRoot(self) -> bool:
return self.parent is None
class LeafNode(Node):
def __init__(self, order):
super().__init__(order)
self.prevLeaf: LeafNode = None
self.nextLeaf: LeafNode = None
# TODO: Implement an improved version
def add(self, key, value):
if not self.keys: # Insert key if it doesn't exist
self.keys.append(key)
self.values.append([value])
return
for i, item in enumerate(self.keys): # Otherwise, search key and append value.
if key == item: # Key found => Append Value
self.values[i].append(value) # Remember, this is a list of data. Not nodes!
break
elif key < item: # Key not found && key < item => Add key before item.
self.keys = self.keys[:i] + [key] + self.keys[i:]
self.values = self.values[:i] + [[value]] + self.values[i:]
break
elif i + 1 == len(self.keys): # Key not found here. Append it after.
self.keys.append(key)
self.values.append([value])
break
def split(self) -> Node: # Split a full leaf node. (Different method used than before!)
top = Node(self.order)
right = LeafNode(self.order)
mid = int(self.order // 2)
self.parent = right.parent = top
right.keys = self.keys[mid:]
right.values = self.values[mid:]
right.prevLeaf = self
right.nextLeaf = self.nextLeaf
top.keys = [right.keys[0]]
top.values = [self, right] # Setup the pointers to child nodes.
self.keys = self.keys[:mid]
self.values = self.values[:mid]
self.nextLeaf = right # Setup pointer to next leaf
return top # Return the 'top node'
class BPlusTree(object):
def __init__(self, order=5):
self.root: Node = LeafNode(order) # First node must be leaf (to store data).
self.order: int = order
@staticmethod
def _find(node: Node, key):
for i, item in enumerate(node.keys):
if key < item:
return node.values[i], i
elif i + 1 == len(node.keys):
return node.values[i + 1], i + 1 # return right-most node/pointer.
@staticmethod
def _mergeUp(parent: Node, child: Node, index):
parent.values.pop(index)
pivot = child.keys[0]
for c in child.values:
if isinstance(c, Node):
c.parent = parent
for i, item in enumerate(parent.keys):
if pivot < item:
parent.keys = parent.keys[:i] + [pivot] + parent.keys[i:]
parent.values = parent.values[:i] + child.values + parent.values[i:]
break
elif i + 1 == len(parent.keys):
parent.keys += [pivot]
parent.values += child.values
break
def insert(self, key, value):
node = self.root
while not isinstance(node, LeafNode): # While we are in internal nodes... search for leafs.
node, index = self._find(node, key)
# Node is now guaranteed a LeafNode!
node.add(key, value)
while len(node.keys) == node.order: # 1 over full
if not node.isRoot():
parent = node.parent
node = node.split() # Split & Set node as the 'top' node.
jnk, index = self._find(parent, node.keys[0])
self._mergeUp(parent, node, index)
node = parent
else:
node = node.split() # Split & Set node as the 'top' node.
self.root = node # Re-assign (first split must change the root!)
def retrieve(self, key):
node = self.root
while not isinstance(node, LeafNode):
node, index = self._find(node, key)
for i, item in enumerate(node.keys):
if key == item:
return node.values[i]
return None
def delete(self, key):
node = self.root
while not isinstance(node, LeafNode):
node, parentIndex = self._find(node, key)
if key not in node.keys:
return False
index = node.keys.index(key)
node.values[index].pop() # Remove the last inserted data.
if len(node.values[index]) == 0:
node.values.pop(index) # Remove the list element.
node.keys.pop(index)
while node.isUnderflow() and not node.isRoot():
# Borrow attempt:
prevSibling = BPlusTree.getPrevSibling(node)
nextSibling = BPlusTree.getNextSibling(node)
jnk, parentIndex = self._find(node.parent, key)
if prevSibling and not prevSibling.isNearlyUnderflow():
self._borrowLeft(node, prevSibling, parentIndex)
elif nextSibling and not nextSibling.isNearlyUnderflow():
self._borrowRight(node, nextSibling, parentIndex)
elif prevSibling and prevSibling.isNearlyUnderflow():
self._mergeOnDelete(prevSibling, node)
elif nextSibling and nextSibling.isNearlyUnderflow():
self._mergeOnDelete(node, nextSibling)
node = node.parent
if node.isRoot() and not isinstance(node, LeafNode) and len(node.values) == 1:
self.root = node.values[0]
self.root.parent = None
@staticmethod
def _borrowLeft(node: Node, sibling: Node, parentIndex):
if isinstance(node, LeafNode): # Leaf Redistribution
key = sibling.keys.pop(-1)
data = sibling.values.pop(-1)
node.keys.insert(0, key)
node.values.insert(0, data)
node.parent.keys[parentIndex - 1] = key # Update Parent (-1 is important!)
else: # Inner Node Redistribution (Push-Through)
parent_key = node.parent.keys.pop(-1)
sibling_key = sibling.keys.pop(-1)
data: Node = sibling.values.pop(-1)
data.parent = node
node.parent.keys.insert(0, sibling_key)
node.keys.insert(0, parent_key)
node.values.insert(0, data)
@staticmethod
def _borrowRight(node: LeafNode, sibling: LeafNode, parentIndex):
if isinstance(node, LeafNode): # Leaf Redistribution
key = sibling.keys.pop(0)
data = sibling.values.pop(0)
node.keys.append(key)
node.values.append(data)
node.parent.keys[parentIndex] = sibling.keys[0] # Update Parent
else: # Inner Node Redistribution (Push-Through)
parent_key = node.parent.keys.pop(0)
sibling_key = sibling.keys.pop(0)
data: Node = sibling.values.pop(0)
data.parent = node
node.parent.keys.append(sibling_key)
node.keys.append(parent_key)
node.values.append(data)
@staticmethod
def _mergeOnDelete(l_node: Node, r_node: Node):
parent = l_node.parent
jnk, index = BPlusTree._find(parent, l_node.keys[0]) # Reset pointer to child
parent_key = parent.keys.pop(index)
parent.values.pop(index)
parent.values[index] = l_node
if isinstance(l_node, LeafNode) and isinstance(r_node, LeafNode):
l_node.nextLeaf = r_node.nextLeaf # Change next leaf pointer
else:
l_node.keys.append(parent_key) # TODO Verify this
for r_node_child in r_node.values:
r_node_child.parent = l_node
l_node.keys += r_node.keys
l_node.values += r_node.values
@staticmethod
def getPrevSibling(node: Node) -> Node:
if node.isRoot() or not node.keys:
return None
jnk, index = BPlusTree._find(node.parent, node.keys[0])
return node.parent.values[index - 1] if index - 1 >= 0 else None
@staticmethod
def getNextSibling(node: Node) -> Node:
if node.isRoot() or not node.keys:
return None
jnk, index = BPlusTree._find(node.parent, node.keys[0])
return node.parent.values[index + 1] if index + 1 < len(node.parent.values) else None
def printTree(self):
if self.root.isEmpty():
print('The bpt+ Tree is empty!')
return
queue = [self.root, 0] # Node, Height... Not systematic but it works
while len(queue) > 0:
node = queue.pop(0)
height = queue.pop(0)
if not isinstance(node, LeafNode):
queue += self.intersperse(node.values, height + 1)
print('Level ' + str(height), '|'.join(map(str, node.keys)), ' -->\t current -> ', node.uid,
'\t parent -> ',
node.parent.uid if node.parent else None)
def getLeftmostLeaf(self):
if not self.root:
return None
node = self.root
while not isinstance(node, LeafNode):
node = node.values[0]
return node
def getRightmostLeaf(self):
if not self.root:
return None
node = self.root
while not isinstance(node, LeafNode):
node = node.values[-1]
def showAllData(self):
node = self.getLeftmostLeaf()
if not node:
return None
while node:
for node_data in node.values:
print('[{}]'.format(', '.join(map(str, node_data))), end=' -> ')
node = node.nextLeaf
print('Last node')
def showAllDataReverse(self):
node = self.getRightmostLeaf()
if not node:
return None
while node:
for node_data in reversed(node.values):
print('[{}]'.format(', '.join(map(str, node_data))), end=' <- ')
node = node.prevLeaf
print()
@staticmethod
def intersperse(lst, item):
result = [item] * (len(lst) * 2)
result[0::2] = lst
return result
if __name__ == '__main__':
bpt = BPlusTree(order=4)
# Insert
customNo = 10
for i in range(customNo):
bpt.insert(i, randint(i, 5 * i))
bpt.printTree()
bpt.showAllData()
print()
for i in range(int(customNo / 2)):
bpt.delete(i)
bpt.printTree()
bpt.showAllData()
print()