This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rbtree.py
326 lines (281 loc) · 9.21 KB
/
rbtree.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
from tree_plt import save_tree
class RBTreeNode:
def __init__(self, val, color="R"):
self.val = val
self.color = color
self.left = None
self.right = None
self.parent = None
def is_black_node(self):
return self.color == "B"
def is_red_node(self):
return self.color == "R"
def set_black_node(self):
self.color = "B"
def set_red_node(self):
self.color = "R"
def print(self):
if self.left:
self.left.print()
print(self.val)
if self.right:
self.right.print()
class RBTree:
def __init__(self):
self.root = None
self.index = -1
self.action = ""
# self.snapshot(snap)
def snapshot(self, snap=True):
if snap:
self.index += 1
save_tree(self.root, "{}".format(self.index))
def left_rotate(self, node):
parent = node.parent
right = node.right
node.right = right.left
if node.right:
node.right.parent = node
right.left = node
node.parent = right
right.parent = parent
if not parent:
self.root = right
else:
if parent.left == node:
parent.left = right
else:
parent.right = right
pass
def right_rotate(self, node):
parent = node.parent
left = node.left
node.left = left.right
if node.left:
node.left.parent = node
left.right = node
node.parent = left
left.parent = parent
if not parent:
self.root = left
else:
if parent.left == node:
parent.left = left
else:
parent.right = left
pass
def search_node(self, val):
if not self.root:
return False
cur = self.root
while cur:
if cur.val == val:
return True
if cur.val < val:
if not cur.right:
return False
cur = cur.right
continue
if cur.val > val:
if not cur.left:
return False
cur = cur.left
continue
return False
def insert_node(self, node):
if not self.root:
self.root = node
return True
cur = self.root
while cur:
if cur.val < node.val:
if not cur.right:
node.parent = cur
cur.right = node
return True
cur = cur.right
continue
if cur.val > node.val:
if not cur.left:
node.parent = cur
cur.left = node
return True
cur = cur.left
continue
if cur.val == node.val:
return False
pass
def check_node(self, node, snap=True):
if self.root == node:
self.root.set_black_node()
self.snapshot(snap)
return
if self.root == node.parent:
self.root.set_black_node()
return
if node.parent.is_black_node():
return
grand = node.parent.parent
if not grand:
self.check_node(node.parent, snap)
return
if grand.left and grand.left.is_red_node() \
and grand.right and grand.right.is_red_node():
grand.left.set_black_node()
grand.right.set_black_node()
grand.set_red_node()
self.snapshot(snap)
self.check_node(grand, snap)
return
parent = node.parent
if parent.left == node and grand.right == node.parent:
self.right_rotate(node.parent)
self.snapshot(snap)
self.check_node(parent, snap)
return
if parent.right == node and grand.left == node.parent:
parent = node.parent
self.left_rotate(node.parent)
self.snapshot(snap)
self.check_node(parent, snap)
return
parent.set_black_node()
grand.set_red_node()
if parent.left == node and grand.left == node.parent:
self.right_rotate(grand)
self.snapshot(snap)
return
if parent.right == node and grand.right == node.parent:
self.left_rotate(grand)
self.snapshot(snap)
return
self.snapshot(snap)
def add_node(self, node, snap=True):
self.action = 'insert node {}'.format(node.val)
if self.insert_node(node):
self.snapshot(snap)
self.check_node(node, snap)
pass
def check_delete_node(self, node, snap=True):
if self.root == node or node.is_red_node():
return
node_is_left = node.parent.left == node
brother = node.parent.right if node_is_left else node.parent.left
if brother.is_red_node():
if node_is_left:
self.left_rotate(node.parent)
else:
self.right_rotate(node.parent)
self.snapshot(snap)
node.parent.set_red_node()
brother.set_black_node()
self.snapshot(snap)
print("check node delete more ")
self.check_delete_node(node, snap)
return
all_none = not brother.left and not brother.right
all_black = brother.left and brother.right \
and brother.left.is_black_node(
) and brother.right.is_black_node()
if all_none or all_black:
brother.set_red_node()
if node.parent.is_red_node():
node.parent.set_black_node()
self.snapshot(snap)
return
self.check_delete_node(node.parent, snap)
return
brother_same_right_red = node_is_left \
and brother.right and brother.right.is_red_node()
brother_same_left_red = not node_is_left \
and brother.left and brother.left.is_red_node()
if brother_same_right_red or brother_same_left_red:
if node.parent.is_red_node():
brother.set_red_node()
else:
brother.set_black_node()
node.parent.set_black_node()
if brother_same_right_red:
brother.right.set_black_node()
self.left_rotate(node.parent)
else:
brother.left.set_black_node()
self.right_rotate(node.parent)
self.snapshot(snap)
return True
brother_diff_right_red = not node_is_left \
and brother.right and brother.right.is_red_node()
brother_diff_left_red = node_is_left \
and brother.left and brother.left.is_red_node()
if brother_diff_right_red or brother_diff_left_red:
brother.set_red_node()
if brother_diff_right_red:
brother.right.set_black_node()
self.left_rotate(brother)
else:
brother.left.set_black_node()
self.right_rotate(brother)
self.snapshot(snap)
self.check_delete_node(node, snap)
return True
def pre_delete_node(self, node, snap=True):
post_node = self.get_post_node(node)
if post_node:
node.val, post_node.val = post_node.val, node.val
self.snapshot(snap)
return self.pre_delete_node(post_node, snap)
pre_node = self.get_pre_node(node)
if pre_node:
pre_node.val, node.val = node.val, pre_node.val
self.snapshot(snap)
return self.pre_delete_node(pre_node, snap)
return node
def get_pre_node(self, node):
if not node.left:
return None
pre_node = node.left
while pre_node.right:
pre_node = pre_node.right
return pre_node
def get_post_node(self, node):
if not node.right:
return None
post_node = node.right
while post_node.left:
post_node = post_node.left
return post_node
def get_node(self, val):
if not self.root:
return None
node = self.root
while node:
if node.val == val:
break
if node.val > val:
node = node.left
continue
else:
node = node.right
return node
def delete_node(self, val, snap=True):
node = self.get_node(val)
if not node:
print("node error {}".format(val))
return
node = self.pre_delete_node(node)
self.check_delete_node(node, snap)
self.real_delete_node(node, snap)
pass
def real_delete_node(self, node, snap=True):
if self.root == node:
self.root = None
self.snapshot(snap)
return
if node.parent.left == node:
node.parent.left = None
self.snapshot(snap)
return
if node.parent.right == node:
node.parent.right = None
self.snapshot(snap)
return