-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRBTree.py
311 lines (254 loc) · 8.64 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
#!/usr/bin/python
# -*- coding:utf-8 -*-
# Author : Yam
# Data : 2021/3/2 21:49
# 全局宏定义
RED = 0
BLACK = 1
import matplotlib.pyplot as plt
class Node:
def __init__(self, value, color=RED, parent=None):
self.data = value
self.left = None
self.right = None
self.parent = parent
self.color = color
# 用于画图的位置信息
self.x = 0
self.y = 0
def __str__(self):
return "{}(c:{})".format(self.data, self.getColor())
def info(self):
return "{}({})'s papa is {}, lc is {}, rc is {}.".format(
self.data, self.getColor(),
self.parent.data if self.parent else "None",
self.left.data if self.left else "None",
self.right.data if self.right else "None")
def cc(self):
"""change color"""
self.color ^= 1
def getColor(self):
return "k" if self.color else "r"
def leftEdge(self):
if self.left:
return [self.x, self.left.x], [self.y, self.left.y]
return None
def rightEdge(self):
if self.right:
return [self.x, self.right.x], [self.y, self.right.y]
return None
class Tree:
def __init__(self):
self.root = None
def lineorder(self):
"""层序遍历"""
from collections import deque
que = deque()
que.append(self.root)
while que:
current = que[0]
print(current.info())
if current.left:
que.append(current.left)
if current.right:
que.append(current.right)
que.popleft()
def inorder(self, current, nodes=None, height=0):
if current is None:
return
self.inorder(current.left, nodes, height=height + 1)
# 当前结点的操作
if nodes:
nodes["len"] += 1
current.x = nodes["len"]
current.y = height
node = [current.x, current.y, current.data]
nodes[current.getColor()].append(node)
else:
print(current.info())
self.inorder(current.right, nodes, height=height + 1)
if nodes:
if current.left:
nodes["edges"].append(current.leftEdge())
if current.right:
nodes["edges"].append(current.rightEdge())
return
def plot(self, save=False, lw=2, s=1000, fontsize=15):
"""画出红黑树图像"""
nodes = {"len": 0, "k": [], "r": [], "edges": []}
# 中序遍历获得结点
self.inorder(self.root, nodes)
# 打印边
for x, y in nodes["edges"]:
plt.plot(x, y, color="k", linewidth=lw, zorder=0)
# 打印红色结点
for color in ["r", "k"]:
colordict = {"r": "#ff9999", "k": "#cfcfcf"}
for node in nodes[color]:
plt.scatter(node[0], node[1], s=s, c=colordict[color],
alpha=1, edgecolors='k', zorder=2)
plt.text(node[0], node[1], node[2], c="k", fontsize=fontsize,
horizontalalignment='center',
verticalalignment='center')
# 扩大画布
deepthr = max(nodes["r"], key=lambda x: x[1])
deepthk = max(nodes["k"], key=lambda x: x[1])
deepth = max(deepthk, deepthr)[1]
plt.axis('off')
plt.xlim((-1, nodes["len"] + 1))
plt.ylim((deepth + 2, -1))
if save:
plt.savefig("./RBTree.png")
plt.show()
class RBTree(Tree):
def rotateR(self, N):
"""
右旋
P P
\ \
--N-- --A--
/ \ --> / \
A B AL N
/ \ / \
AL AR AR B
"""
assert N.left, "{} with no left child".format(str(N))
A = N.left
A.parent = N.parent
if not N.parent: # 如果N的父节点不存在
self.root = A # A 是新的根结点
elif N.parent.left is N: # N原来是左孩子
N.parent.left = A
elif N.parent.right is N: # N原来是右孩子
N.parent.right = A
# 挂上 AR
if A.right:
A.right.parent = N
N.left = A.right
# A,N父子关系
N.parent = A
A.right = N
def rotateL(self, N):
"""
左旋
P P
\ \
--N-- --B--
/ \ --> / \
A B N BR
/ \ / \
BL BR A BL
"""
assert N.right, "{} with no right child".format(str(N))
B = N.right
B.parent = N.parent
if not N.parent: # 如果N的父节点不存在
self.root = B # A 是新的根结点
elif N.parent.left is N: # N原来是左孩子
N.parent.left = B
elif N.parent.right is N: # N原来是右孩子
N.parent.right = B
# 挂上 BL
if B.left:
B.left.parent = N
N.right = B.left
# B, N父子关系
N.parent = B
B.left = N
def insert(self, value):
"""插入操作"""
new_node = Node(value)
# 寻找插入位置并插入,用 ptr 指向新插入的结点
ptr = self.root
papa = None
while ptr:
papa = ptr
if new_node.data <= papa.data:
ptr = papa.left
else:
ptr = papa.right
if not papa:
# papa 为空,没进入while 循环,即根结点为空
self.root = new_node
new_node.color = BLACK
return
else:
# 插入结点
if new_node.data <= papa.data:
papa.left = new_node
else:
papa.right = new_node
new_node.parent = papa
self.insert_fixup(new_node)
def insert_fixup(self, current):
"""调整current(红结点)"""
papa = current.parent
# 如果current是红结点,并且papa也是红结点
while papa and papa.color == RED:
grandpa = papa.parent
if grandpa.left is papa:
# papa(r) <- grandpa
uncle = grandpa.right
if uncle and uncle.color == RED:
# papa(r) <- grandpa -> uncle(r)
# 变色
papa.cc()
uncle.cc()
grandpa.cc()
current = grandpa
continue
if papa.right is current:
# grandpa(b)
# / \
# papa(r) None or Black Node
# \
# current(r)
self.rotateL(papa)
papa, current = current, papa
# grandpa(b)
# / \
# papa(r) None or Black Node
# /
# current(r)
self.rotateR(grandpa)
grandpa.cc()
papa.cc()
# papa(b)
# / \
# current(r) grandpa(r)
else: # grandpa -> papa(r)
uncle = grandpa.left
if uncle and uncle.color == RED:
# uncle(r) <- grandpa -> papa(r)
papa.cc()
uncle.cc()
grandpa.cc()
current = grandpa
continue
if papa.left is current:
# grandpa(b)
# / \
# ^ papa(r)
# /
# current(r)
self.rotateR(papa)
papa, current = current, papa
# grandpa(b)
# / \
# ^ papa(r)
# \
# current(r)
self.rotateL(grandpa)
grandpa.cc()
papa.cc()
# papa(b)
# / \
# grandpa(r) current(r)
# 确保根结点是黑色
self.root.color = BLACK
if __name__ == '__main__':
rbt = RBTree()
values = [9, 5, 4, 6, 2, 8, 7, 3, 1, 0]
for v in values:
rbt.insert(v)
rbt.plot(save=True)