-
Notifications
You must be signed in to change notification settings - Fork 369
/
Copy pathred_black.py
291 lines (247 loc) · 9.65 KB
/
red_black.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
# Define Node
class Node():
def __init__(self,val):
self.val = val
self.parent = None
self.left = None
self.right = None
self.color = 1
# Define R-B Tree
class RBTree():
def __init__(self):
self.NULL = Node ( 0 )
self.NULL.color = 0
self.NULL.left = None
self.NULL.right = None
self.root = self.NULL
# Insert New Node
def insertNode(self, key):
node = Node(key)
node.parent = None
node.val = key
node.left = self.NULL
node.right = self.NULL
node.color = 1
y = None
x = self.root
while x != self.NULL :
y = x
if node.val < x.val :
x = x.left
else :
x = x.right
node.parent = y
if y == None :
self.root = node
elif node.val < y.val :
y.left = node
else :
y.right = node
if node.parent == None :
node.color = 0
return
if node.parent.parent == None :
return
self.fixInsert ( node )
def minimum(self, node):
while node.left != self.NULL:
node = node.left
return node
# Code for left rotate
def LR ( self , x ) :
y = x.right
x.right = y.left
if y.left != self.NULL :
y.left.parent = x
y.parent = x.parent
if x.parent == None :
self.root = y
elif x == x.parent.left :
x.parent.left = y
else :
x.parent.right = y
y.left = x
x.parent = y
# Code for right rotate
def RR ( self , x ) :
y = x.left
x.left = y.right
if y.right != self.NULL :
y.right.parent = x
y.parent = x.parent
if x.parent == None :
self.root = y
elif x == x.parent.right :
x.parent.right = y
else :
x.parent.left = y
y.right = x
x.parent = y
# Fix Up Insertion
def fixInsert(self, k):
while k.parent.color == 1:
if k.parent == k.parent.parent.right:
u = k.parent.parent.left
if u.color == 1:
u.color = 0
k.parent.color = 0
k.parent.parent.color = 1
k = k.parent.parent
else:
if k == k.parent.left:
k = k.parent
self.RR(k)
k.parent.color = 0
k.parent.parent.color = 1
self.LR(k.parent.parent)
else:
u = k.parent.parent.right
if u.color == 1:
u.color = 0
k.parent.color = 0
k.parent.parent.color = 1
k = k.parent.parent
else:
if k == k.parent.right:
k = k.parent
self.LR(k)
k.parent.color = 0
k.parent.parent.color = 1
self.RR(k.parent.parent)
if k == self.root:
break
self.root.color = 0
# fixing problems after deletion
def fixDelete ( self , x ) :
while x != self.root and x.color == 0 :
if x == x.parent.left :
s = x.parent.right
if s.color == 1 :
s.color = 0
x.parent.color = 1
self.LR ( x.parent )
s = x.parent.right
# If both the child are black
if s.left.color == 0 and s.right.color == 0 :
s.color = 1
x = x.parent
else :
if s.right.color == 0 :
s.left.color = 0
s.color = 1
self.RR ( s )
s = x.parent.right
s.color = x.parent.color
x.parent.color = 0
s.right.color = 0
self.LR ( x.parent )
x = self.root
else :
s = x.parent.left
if s.color == 1 :
s.color = 0
x.parent.color = 1
self.RR ( x.parent )
s = x.parent.left
if s.right.color == 0 and s.right.color == 0 :
s.color = 1
x = x.parent
else :
if s.left.color == 0 :
s.right.color = 0
s.color = 1
self.LR ( s )
s = x.parent.left
s.color = x.parent.color
x.parent.color = 0
s.left.color = 0
self.RR ( x.parent )
x = self.root
x.color = 0
# Function to transplant nodes
def __rb_transplant ( self , u , v ) :
if u.parent == None :
self.root = v
elif u == u.parent.left :
u.parent.left = v
else :
u.parent.right = v
v.parent = u.parent
# Function for deletion
def delete_node_helper ( self , node , key ) :
z = self.NULL
while node != self.NULL :
if node.val == key :
z = node
if node.val <= key :
node = node.right
else :
node = node.left
if z == self.NULL :
print ( "VALUE NOT PRESENT IN TREE !!" )
return
y = z
y_original_color = y.color
if z.left == self.NULL :
x = z.right
self.__rb_transplant ( z , z.right )
elif (z.right == self.NULL) :
x = z.left
self.__rb_transplant ( z , z.left )
else :
y = self.minimum ( z.right )
y_original_color = y.color
x = y.right
if y.parent == z :
x.parent = y
else :
self.__rb_transplant ( y , y.right )
y.right = z.right
y.right.parent = y
self.__rb_transplant ( z , y )
y.left = z.left
y.left.parent = y
y.color = z.color
if y_original_color == 0 :
self.fixDelete ( x )
# Deletion of node
def delete_node ( self , val ) :
self.delete_node_helper ( self.root , val )
# Function to print
def __printCall ( self , node , indent , last ) :
if node != self.NULL :
print(indent, end=' ')
if last :
print ("R----",end= ' ')
indent += " "
else :
print("L----",end=' ')
indent += "| "
s_color = "RED" if node.color == 1 else "BLACK"
print ( str ( node.val ) + "(" + s_color + ")" )
self.__printCall ( node.left , indent , False )
self.__printCall ( node.right , indent , True )
# Function to call print
def print_tree ( self ) :
self.__printCall ( self.root , "" , True )
tree = RBTree()
print("Select the operation you want to perform: ")
print("1. INSERT NODE")
print("2. DELETE NODE")
print("3. DISPLAY")
print("4. EXIT")
exit = True
while exit:
choice = int(input("ENTER THE OPERATION NUMBER YOU WANT TO PERFORM: "))
if(choice == 1):
data = int(input("ENTER THE DATA YOU WANT TO INSERT INTO TREE: "))
tree.insertNode(data)
elif(choice == 2):
val = int(input("ENTER THE NODE VALUE WHICH YOU WANT TO DELETE: "))
tree.delete_node(val)
elif(choice == 3):
tree.print_tree()
elif(choice == 4):
exit = False
else:
print("INVALID INPUT!!!")