forked from tlux01/CS591-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRBBTree.py
417 lines (345 loc) · 12.3 KB
/
RBBTree.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import random
#don't touch
LEFT = 0
RIGHT = 1
# Balance Binary Tree Class
class RBBTree:
def __init__(self):
self.parent = None
self.child = [None, None]
# priority used in balancing tree
self.priority = random.random()
# starting at this node, walk down as far left
def first(self):
#current node in traversal
cur = self
while cur.child[LEFT]:
cur = cur.child[LEFT]
return cur
# starting at this node, walk down as far right
def last(self):
#current node in traversal
cur = self
while cur.child[RIGHT]:
cur = cur.child[RIGHT]
return cur
# follow parent up the tree as long as possible
def find_root(self):
root = self
while root.parent:
root = root.parent
return root
#isolates this node, takes care of child and parent pointers
def isolate(self):
# false if no parent
# isolate from parent
if self.parent:
if self.parent.child[LEFT] is self:
self.parent.child[LEFT] = None
else:
self.parent.child[RIGHT] = None
# Isolate from children
if self.child[LEFT]:
self.child[LEFT].parent = None
if self.child[RIGHT]:
self.child[RIGHT].parent = None
# remove children
self.child
# find successor in InOrder Traversal of InOrder
# returns this node if exists, None otherwise
def successor(self):
sub_successor = None
# if there is a right sub tree
if self.child[RIGHT]:
# go into right sub tree
cur = self.child[RIGHT]
sub_successor = cur
# then go left as far as possible as this will be successor
while cur:
sub_successor = cur
cur = cur.child[LEFT]
# if no sub_successor, need to go up through parent and check if successor exists there
# otherwise return this sub_successor
if sub_successor:
return sub_successor
else:
# check if parent exists
if self.parent:
# if it is the left child
if self is self.parent.child[LEFT]:
#successor in Inorder traversal is then the parent
return self.parent
# is right child
else:
cur = self.parent
while cur and cur.parent:
# keep going up to parent until we find
# that our parent is a left node, return the parent of this parent
if cur is cur.parent.child[LEFT]:
return cur.parent
cur = cur.parent
# if we get here we return None
return None
# find predecessor in InOrder Traversal of InOrder
# returns this node if exists, None otherwise
def predecessor(self):
sub_predecessor = None
# if there is a left sub tree
if self.child[LEFT]:
# go into left sub tree
cur = self.child[LEFT]
sub_predecessor = cur
# then go right as far as possible as this will be successor
while cur:
sub_predecessor = cur
cur = cur.child[RIGHT]
# if no sub_predecessor, need to go up through parent and check if predecessor exists there
# otherwise return this sub_predecessor
if sub_predecessor:
return sub_predecessor
else:
# check if parent exists
if self.parent:
# if it is the right child
if self is self.parent.child[RIGHT]:
#predecessor in Inorder traversal is then the parent
return self.parent
# is left child
else:
cur = self.parent
# check if there is parent
while cur and cur.parent:
# keep going up to parent until we find
# that our parent is a right node, return the parent of this parent
if cur is cur.parent.child[RIGHT]:
return cur.parent
cur = cur.parent
# if we get here we return None
return None
def cyclic_pred(self):
c_pred = self.last() if self is self.first() else self.predecessor()
return c_pred
def cyclic_succ(self):
c_succ = self.first() if self is self.last() else self.successor()
return c_succ
def after_rot(self):
pass
def init(self):
pass
# returns the in-order list of nodes
def in_order(self):
accum = [self]
if self.child[LEFT]:
accum = self.child[LEFT].in_order() + accum
if self.child[RIGHT]:
accum = accum + self.child[RIGHT].in_order()
return accum
################### STATIC METHODS to operate on our RBBTREE #########################
# rotate a tree for balancing, does not change InOrder traversal of tree
# rotate depends if child is left or right child of parent
def rotate(r_child, r_parent):
#print("rotate child:{} with parent{}:".format(r_child, r_parent))
rotation_direction = RIGHT if r_parent.child[LEFT] is r_child else LEFT
mid_tree = r_child.child[rotation_direction]
# move mid tree to opposite side of child of parent
r_parent.child[1 - rotation_direction] = mid_tree
if mid_tree:
mid_tree.parent = r_parent
# assign child the parent of its parent
r_child.parent = r_parent.parent
# update this new parent to replace its child which was
# r_parent to be r_child
if r_child.parent:
if (r_child.parent.child[LEFT] is r_parent):
r_child.parent.child[LEFT] = r_child
else:
r_child.parent.child[RIGHT] = r_child
# rotate parent to be child of child in direction
r_child.child[rotation_direction] = r_parent
r_parent.parent = r_child
# fix additional information in derived classes
r_parent.after_rot()
# returns true if node u is before v in the InOrder traversal, false otherwise
def smaller(u, v):
# if u or v = None
if not u or not v:
return False
# if they are the same also return false
if u is v:
return False
# get height of u
height_u = 0
cur_u = u
while cur_u.parent:
height_u += 1
cur_u = cur_u.parent
# get height of v
height_v = 0
cur_v = v
while cur_v.parent:
height_v += 1
cur_v = cur_v.parent
#case where they have different root, then we can't determine smaller
if cur_u is not cur_v:
return False
# paths from root
u_path = []
v_path = []
# construct paths in term of Lefts and Rights from root
cur_u = u
while cur_u.parent:
if cur_u.parent.child[LEFT] is cur_u:
#build path bottom up, first index will be path starting from root
u_path = [LEFT] + u_path
else:
u_path = [RIGHT] + u_path
cur_u = cur_u.parent
cur_v = v
while cur_v.parent:
if cur_v.parent.child[LEFT] is cur_v:
#build path bottom up, first index will be path starting from root
v_path = [LEFT] + v_path
else:
v_path = [RIGHT] + v_path
cur_v = cur_v.parent
#we compare paths such that the one that is most left is the one that will be before in Inorder
i = 0
# we find index of first difference in paths
while i < height_u and i < height_v:
if u_path[i] != v_path[i]:
break
i += 1
# if we have not reached end of path u, and step at i is LEFT for u, then u is more left than v
if i < height_u and u_path[i] == LEFT:
return True
# if we have not reach end of path v, and step at i is RIGHT for v, v is more right than u
elif i < height_v and v_path[i] == RIGHT:
return True
else:
return False
# join two trees with the correct InOrder based on their priority
def join(t1, t2, dummy):
#print("join")
if not t1 or not t2:
if t1:
return t1
elif t2:
return t2
else:
return None
# make dummy the root of both trees
dummy.parent = None
dummy.child[LEFT] = t1
dummy.child[RIGHT] = t2
t1.parent = dummy
t2.parent = dummy
#fix info for derived classes
dummy.init()
#print("Dummy after init:", dummy)
# rotate dummy down until it is a leaf
while dummy.child[LEFT] or dummy.child[RIGHT]:
# to preserve in order we rotate with the node down
larger = None
left = dummy.child[LEFT]
right = dummy.child[RIGHT]
# check if right child exists
if right:
# check that left child exists
if left:
if right.priority > left.priority:
larger = right
else:
larger = left
else:
larger = right
# no right child so default to left
else:
larger = left
# Now that we have found larger child, we rotate it with dummy
rotate(larger, dummy)
# remove dummy from tree
dummy.isolate()
#if t1, which is root of t1, does have parent then t2 is the root
if t1.parent:
return t2
else:
return t1
#starting at our start_node, return two trees either split starting from the LEFT or RIGHT of this node
def split(start_node, direction, dummy):
#print("splitting")
if not start_node:
t1 = None
t2 = None
return t1, t2
dummy.child[LEFT] = None
dummy.child[RIGHT] = None
# we want to add dummy node in manner where we don't cut off and part
# of the tree, and maintains InOrder of our tree, rotating dummy up until it
# replaces our root, where we then can isolate dummy creating two split
# trees
# split after our start node, t1 contains start node
if(direction == RIGHT):
sub_successor = None
if start_node.child[RIGHT]:
cur = start_node.child[RIGHT]
sub_successor = cur
while cur:
sub_successor = cur
cur = cur.child[LEFT]
if not sub_successor:
#None to right of start node, so replace with dummy
start_node.child[RIGHT] = dummy
dummy.parent = start_node
else:
# store dummy as left child of subtree successor which is immediately after our start node
# as this is always None, sub_successor does not have a right child by def
sub_successor.child[LEFT] = dummy
dummy.parent = sub_successor
# split before our start node, t1 does not contain start node
else:
sub_predecessor = None
if start_node.child[LEFT]:
cur = start_node.child[LEFT]
sub_predecessor = cur
while cur:
sub_predecessor = cur
cur = cur.child[RIGHT]
if not sub_predecessor:
# None at left child so replace that with dummy
start_node.child[LEFT] = dummy
dummy.parent = start_node
else:
# store dummy as right child of subtree predecessor which is immediately before our start node
# as this is always None, sub_predecessor does not have a right child by def
sub_predecessor.child[RIGHT] = dummy
dummy.parent = sub_predecessor
# for derived classes
dummy.init()
#rotate dummy until it becomes root
while dummy.parent:
p = dummy.parent
#print(p)
rotate(dummy, p)
t1 = dummy.child[LEFT]
t2 = dummy.child[RIGHT]
dummy.isolate()
return t1, t2
# for level order traversal
def height(root):
if not root:
return 0
return 1 + max(height(root.child[LEFT]), height(root.child[RIGHT]))
# levelorder traversal from root
def print_tree(root):
h = height(root)
for i in range(1, h+ 1):
_print_tree(root, i)
print("")
def _print_tree(root, level):
if not root:
return root
if level == 1:
print(root, end = ' ')
elif level > 1:
_print_tree(root.child[LEFT], level - 1)
_print_tree(root.child[RIGHT], level - 1)