-
Notifications
You must be signed in to change notification settings - Fork 1
/
project3a_BTree.py
347 lines (286 loc) · 10.5 KB
/
project3a_BTree.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
# Refrences: https://iq.opengenus.org/b-tree-in-cpp/
# Reference: https://pythonexamples.org/python-list-of-dictionaries/
class BTreeNode:
def __init__(self, leaf=False):
self.leaf = leaf
self.keys = []
self.child = []
class BTree:
def __init__(self, order):
self.root = BTreeNode(True)
#'order' is order of B Tree
self.order = order
"""Get Keys' value-value at the key 'k', value 'v'.
Returns 'None' if 'k' is not found.
Otherwise return k's tuple value at which the value was found.
Arguments:
k -- key to be searched
v -- key's value-key to be searched
"""
def get_keys_value(self, k, v):
K = self.search(hash(k))
if K != None:
(x, i) = K
dict = x.keys[i][2]
return dict.get(v)
"""Update Keys value at the key 'k'.
Returns 'None' if 'k' is not found.
Otherwise go to k's tuple value at which the value was found.
Arguments:
k -- key to be searched
v -- key's value-key to be searched
uv -- new value to be inserted
"""
def update_keys(self, k, v, uv):
K = self.search(hash(k))
if K != None:
(x, i) = K
dict = x.keys[i][2]
dict.update({v: uv})
tpl = list(x.keys[i])
tpl[2] = dict
x.keys[i] = tuple(tpl)
"""Pull Node Info for key 'k'.
Returns 'None' if 'k' is not found.
Otherwise returns a dictionary information at which the key was found.
Arguments:
k -- key to be searched
"""
def pull_node_info(self, k):
K = self.search(hash(k))
if K != None:
(x, i) = K
print("The studentID has following: ", x.keys[i][2])
"""Search for key 'k' at position 'x'.
If 'x' is not specified, then search occurs from root.
Returns 'None' if 'k' is not found.
Otherwise returns a tuple of node and index at which the key was found.
Arguments:
k -- key to be searched
x -- position to search from
"""
def search(self, k, x=None):
if x != None:
i = 0
while i < len(x.keys) and k > x.keys[i][0]:
i += 1
if i < len(x.keys) and k == x.keys[i][0]:
return (x, i)
elif x.leaf:
return None
else:
# Search in children
return self.search(k, x.child[i])
else:
# Search entire tree as node not provided
return self.search(k, self.root)
"""Insert key 'k' at position 'x' in a non-full node
Arguments:
x -- Position of node
k -- key to be inserted
"""
def insert_nonfull(self, x, k):
i = len(x.keys) - 1
if x.leaf:
x.keys.append((None, None))
while i >= 0 and k[0] < x.keys[i][0]:
x.keys[i + 1] = x.keys[i]
i -= 1
x.keys[i + 1] = k
else:
while i >= 0 and k[0] < x.keys[i][0]:
i -= 1
i += 1
if len(x.child[i].keys) == (2 * self.order) - 1:
self.split_child(x, i)
if k[0] > x.keys[i][0]:
i += 1
self.insert_nonfull(x.child[i], k)
"""Splits the child of node at 'x' from index 'i'
Arguments:
x -- parent node of the node to be split
i -- index value of the child
"""
def split_child(self, x, i):
order = self.order
y = x.child[i]
z = BTreeNode(y.leaf)
x.child.insert(i + 1, z)
x.keys.insert(i, y.keys[order - 1])
z.keys = y.keys[order : (2 * order) - 1]
y.keys = y.keys[0 : order - 1]
if not y.leaf:
z.child = y.child[order : 2 * order]
y.child = y.child[0 : order - 1]
"""Insert key 'k' in the B-Tree
Arguments:
k -- key to be inserted
"""
def insert(self, k):
root = self.root
# Keys are full, hence we must split child
if len(root.keys) == (2 * self.order) - 1:
temp = BTreeNode()
self.root = temp
# Former root becomes 0th child of new root 'temp'
temp.child.insert(0, root)
self.split_child(temp, 0)
self.insert_nonfull(temp, k)
else:
self.insert_nonfull(root, k)
"""Deletes internal node
Arguments:
x -- internal node in which key 'k' is present
k -- key to be deleted
i -- index position of key in the list
"""
def delete_internal_node(self, x, k, i):
order = self.order
# Deleting the key if the node is a leaf
if x.leaf:
if x.keys[i][0] == k[0]:
x.keys.pop(i)
return
return
# Replacing the key with its predecessor and deleting predecessor
if len(x.child[i].keys) >= order:
x.keys[i] = self.delete_predecessor_node(x.child[i])
return
# Replacing the key with its successor and deleting successor
elif len(x.child[i + 1].keys) >= order:
x.keys[i] = self._delete_successor(x.child[i + 1])
return
# Merging the child, its left sibling and the key 'k'
else:
self.delete_merge(x, i, i + 1)
self.delete_internal_node(x.child[i], k, self.order - 1)
"""Returns and deletes predecessor of key 'k' which is to be deleted
Arguments:
x -- node
"""
def delete_predecessor_node(self, x):
if x.leaf:
return x.pop()
n = len(x.keys) - 1
if len(x.child[n].keys) >= self.order:
self.delete_sibling(x, n + 1, n)
else:
self.delete_merge(x, n, n + 1)
self.delete_predecessor_node_node(x.child[n])
"""Returns and deletes successor of key 'k' which is to be deleted
Arguments:
x -- node
"""
def delete_successor_node(self, x):
if x.leaf:
return x.keys.pop(0)
if len(x.child[1].keys) >= self.order:
self.delete_sibling(x, 0, 1)
else:
self.delete_merge(x, 0, 1)
self.delete_successor_node(x.child[0])
"""Merges the children of x and one of its own keys
Arguments:
x -- parent node
i -- index of one of the children
j -- index of one of the children
"""
def delete_merge(self, x, i, j):
cnode = x.child[i]
# Merging the x.child[i], x.child[j] and x.keys[i]
if j > i:
rsnode = x.child[j]
cnode.keys.append(x.keys[i])
# Assigning keys of right sibling node to child node
for k in range(len(rsnode.keys)):
cnode.keys.append(rsnode.keys[k])
if len(rsnode.child) > 0:
cnode.child.append(rsnode.child[k])
if len(rsnode.child) > 0:
cnode.child.append(rsnode.child.pop())
new = cnode
x.keys.pop(i)
x.child.pop(j)
# Merging the x.child[i], x.child[j] and x.keys[i]
else:
lsnode = x.child[j]
lsnode.keys.append(x.keys[j])
# Assigning keys of left sibling node to child node
for i in range(len(cnode.keys)):
lsnode.keys.append(cnode.keys[i])
if len(lsnode.child) > 0:
lsnode.child.append(cnode.child[i])
if len(lsnode.child) > 0:
lsnode.child.append(cnode.child.pop())
new = lsnode
x.keys.pop(j)
x.child.pop(i)
# If x is root and is empty, then re-assign root
if x == self.root and len(x.keys) == 0:
self.root = new
"""Borrows a key from jth child of x and appends it to ith child of x
Arguments:
x -- parent node
i -- index of one of the children
j -- index of one of the children
"""
def delete_sibling(self, x, i, j):
cnode = x.child[i]
if i < j:
# Borrowing key from right sibling of the child
rsnode = x.child[j]
cnode.keys.append(x.keys[i])
x.keys[i] = rsnode.keys[0]
if len(rsnode.child) > 0:
cnode.child.append(rsnode.child[0])
rsnode.child.pop(0)
rsnode.keys.pop(0)
else:
# Borrowing key from left sibling of the child
lsnode = x.child[j]
cnode.keys.insert(0, x.keys[i - 1])
x.keys[i - 1] = lsnode.keys.pop()
if len(lsnode.child) > 0:
cnode.child.insert(0, lsnode.child.pop())
"""Calls helper functions to delete key 'k' after searching from node 'x'
Arguments:
x -- node, according to whose relative position, helper functions are called
k -- key to be deleted
"""
def delete(self, x, k):
order = self.order
i = 0
while i < len(x.keys) and k > x.keys[i][0]:
i += 1
# Deleting the key if the node is a leaf
if x.leaf:
if i < len(x.keys) and x.keys[i][0] == k:
x.keys.pop(i)
return
return
# Calling 'delete_internal_node' when x is an internal node and contains the key 'k'
if i < len(x.keys) and x.keys[i][0] == k:
return self.delete_internal_node(x, k, i)
# Recursively calling 'delete' on x's child
elif len(x.child[i].keys) >= order:
self.delete(x.child[i], k)
# Ensuring that a child always has atleast 'order' keys
else:
if i != 0 and i + 2 < len(x.child):
if len(x.child[i - 1].keys) >= order:
self.delete_sibling(x, i, i - 1)
elif len(x.child[i + 1].keys) >= order:
self.delete_sibling(x, i, i + 1)
else:
self.delete_merge(x, i, i + 1)
elif i == 0:
if len(x.child[i + 1].keys) >= order:
self.delete_sibling(x, i, i + 1)
else:
self.delete_merge(x, i, i + 1)
elif i + 1 == len(x.child):
if len(x.child[i - 1].keys) >= order:
self.delete_sibling(x, i, i - 1)
else:
self.delete_merge(x, i, i - 1)
self.delete(x.child[i], k)