-
Notifications
You must be signed in to change notification settings - Fork 0
/
redBlackTree.py
309 lines (253 loc) · 10.7 KB
/
redBlackTree.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
'''
This file demonstrates the construction and use of a red-black tree. Recall that a red-black
tree is a type of balanced binary search tree that uses coloring of nodes in order to facilitate in
the balancing process of a binary search tree.
Recall that the properties of a red black tree are as follows:
- Root property - root node should be black
- Leaf property - every leaf node should be black
- Red property - The children of a red node should be black
- Depth property - All paths to a leaf node should have the same black node depth
In this file we set up two demo red black trees and then show what the tree looks like during left and right rotations.
'''
from print_tree import print_tree_from_breadth_first_stack
class RedBlackNode(object):
color = None
value = None
left_node = None
right_node = None
parent = None
nil = False
#setter methods to be referenced by property methods below
def setParent(self, parent):
self._parent = parent
def setLeft(self, left):
self._left = left
def setRight(self, right):
self._right = right
def setValue(self, value):
self._value = value
def setNil(self, nil):
self._nil = nil
def setColor(self, color):
self._color = color
#property methods
parent = property(fget=lambda self: self._parent if self._parent!=None else RedBlackNode(None, True),fset=setParent)
left = property(fget=lambda self: self._left if self._left!=None else RedBlackNode(None, True), fset = setLeft)
right= property(fget=lambda self: self._right if self._right!=None else RedBlackNode(None, True), fset = setRight)
value = property(fget=lambda self: self._value ,fset=setValue)
nil = property(fget=lambda self: self._nil, fset=setNil)
color = property(fget=lambda self: self._color ,fset=setColor)
def __init__(self, value=None, nil=False):
self._color = "black"
self._value = value
self._left = None
self._right = None
self._parent = None
self._nil=nil
#red black tree to represent tree to hold red black nodes
class RedBlackTree(object):
_root = None
def set_root(self, newroot):
self._root = newroot
root = property(fget=lambda self: self._root, fset=set_root)
def reset_nodes(self, number_of_nodes):
'''
Set up demo nodes with values 1 to 15
'''
self.nodes_array = []
for i in range(0, number_of_nodes+1):
self.nodes_array.append(RedBlackNode(value=i))
self.root = None
def insert_node(self, current, newnode):
'''
Insert a node the same way that you would in a standard binary search tree.
This is to be later rotated to be in line with properties of a red black tree.
'''
if self.root == None:
self.root = newnode
return
#if both are null return
if current.left.nil == True and current.right.nil==True:
if newnode.value < current.value:
current.left = newnode
newnode.parent = current
return
else:
current.right = newnode
newnode.parent = current
return
if newnode.value < current.value:
if current.left.nil == True:
current.left = newnode
newnode.parent = current
else:
self.insert_node(current.left, newnode)
else:
if current.right.nil == True:
current.right = newnode
newnode.parent = current
else:
self.insert_node(current.right, newnode)
def traverse_tree(self, current):
print "value: " + str(current.value) + " color: " + str(current.color)
#base case when you have reached a leaf node
if current.left.nil == True and current.right.nil == True:
return
if current.left.nil==False:
print "going left from " + str(current.value) + " color: " + str(current.color)
self.traverse_tree(current.left)
if current.right.nil == False:
print "going right from " + str(current.value) + " color: " + str(current.color)
self.traverse_tree(current.right)
def print_tree(self):
stack = self.traverse_tree_by_levels()
print_tree_from_breadth_first_stack(stack, print_method="red_black_node")
def traverse_tree_by_levels(self):
stack = self.tree_level_visit(self.root)
if stack == None:
return None
return [node for node in stack]
def tree_level_visit(self, current):
stack = []
if current == None:
return
stack.append(current)
stack.append(current.left)
stack.append(current.right)
for i in range(1, len(stack)):
stack.append(stack[i].left)
stack.append(stack[i].right)
return stack
def rotate_left(self, xnode):
'''
Performs a left rotate around the xnode
'''
############get Y node, get y node left, set x right as y left, set ynode parent to x parent#################
ynode = xnode.right
xnode.right = ynode.left
if ynode.left.nil != True:
ynode.left.parent = xnode
#set y parent to x parent
ynode.parent = xnode.parent
#######################################
#######################################
#below if else block to set right or left of parent to ynode or set as root if no parent
# or set y as root
#check to see if at root first
if xnode.parent.nil == True:
self.root = ynode
elif xnode == xnode.parent.left:
xnode.parent.left = ynode
else: #x must have been on right
xnode.parent.right = ynode
#######################################
#finally put y above x and set x as its left node
ynode.left = xnode
xnode.parent = ynode
def rotate_right(self,ynode):
'''
#performs a right rotate around the ynode
'''
############get x node, get x node right, set y left as x right, set parent of xnode to y parent#################
xnode = ynode.left
ynode.left = xnode.right
if xnode.right.nil!= True:
xnode.right.parent = ynode
#set x parent to y parent
xnode.parent = ynode.parent
#######################################
#######################################
#below if block to set ynode new parent to xnode as right or left, or set x as theroot
#check to see if at root first
if (ynode.parent.nil == True):
self.root = xnode
elif ynode == ynode.parent.right:
ynode.parent.right = xnode
else: #y must have been on right
ynode.parent.left = xnode
#######################################
#finally put x above y and set y as its right node
xnode.right = ynode
ynode.parent = xnode
def insert_red_black(self, newnode):
'''
Insert a node into a red black tree using standard binary search tree insertion and then
update nodes to be congruent with properties of a red black tree.
'''
newnode.color = "red"
#insert node in the standard binary search tree way
self.insert_node(self.root, newnode)
while newnode.parent.color == "red":
#newnode parent on left of its parent
if newnode.parent == newnode.parent.parent.left:
#case 1: if newnodes right uncle is red
if newnode.parent.parent.right.color == "red":
newnode.parent.color = "black"
newnode.parent.parent.right.color = "black"
newnode.parent.parent.color = "red"
newnode = newnode.parent.parent
else: #else newnodes uncle must be black or nonexist
#case 2: newnode right uncle black and a right child
if newnode == newnode.parent.right:
newnode = newnode.parent
self.rotate_left(newnode)
#newnode right uncle black and newnode is left child
newnode.parent.color = "black"
newnode.parent.parent.color = "red"
self.rotate_right(newnode.parent.parent)
#begin symmetric for cases 4-6
elif newnode.parent == newnode.parent.parent.right:
#case 4 if left uncle is red
if newnode.parent.parent.left.color =="red":
newnode.parent.color = "black"
newnode.parent.parent.left.color = "black"
newnode.parent.parent.color = "red"
newnode = newnode.parent.parent
else: #left uncle is nonexistent or black
#case 5 newnode left uncle black, new node left child
if newnode == newnode.parent.left:
newnode = newnode.parent
self.rotate_right(newnode)
#case 6 newnode left uncle black, newnode right child
newnode.parent.color = "black"
newnode.parent.parent.color = "red"
self.rotate_left(newnode.parent.parent)
self.root.color = "black"
return newnode
def setup_nodes_demo(self, node_insertion_list):
for num in node_insertion_list:
self.insert_red_black(self.nodes_array[num])
def node_insert_demo(self, node_insertion_list):
for num in node_insertion_list:
print ":::::: before insertion of: " + str(num) + " ::::::"
self.print_tree()
self.insert_red_black(self.nodes_array[num])
print ":::::: after insertion of: " + str(num) + "::::::"
self.print_tree()
redandblack = RedBlackTree()
node_list1 = [4, 3, 5, 2, 7, 6, 8, 9, 10 ,11]
node_list2 = [11, 2, 14, 1, 7, 15, 5, 8, 4]
node_list3 = [5, 3, 7, 1, 9, 4, 6]
print "------ Red Black Tree Demo 1 ------"
print "inserting the following nodes in order: "
print str(node_list1)
redandblack.reset_nodes(15)
redandblack.setup_nodes_demo(node_list1)
print "--- Red Black Tree 1 is ---"
redandblack.print_tree()
print ""
print ""
print "--------- Red Black Tree Demo 2 ---------"
print "inserting the following nodes in order:"
print str(node_list2)
redandblack.reset_nodes(15)
redandblack.setup_nodes_demo(node_list2)
print "--------- Red Black Tree 2 is ---------"
redandblack.print_tree()
print ""
print ""
print "--------- Red Black Tree print after each insertion ---------"
print "inserting the following nodes in order: "
print str(node_list3)
redandblack.reset_nodes(15)
redandblack.node_insert_demo(node_list3)