forked from heineman/IntroductionAlgorithmsDataStructures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavl.py
276 lines (226 loc) · 8.21 KB
/
avl.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
"""
AVL Implementation of Binary Tree.
Implementation taken from
https://github.com/heineman/python-data-structures
"""
class BinaryNode:
def __init__(self, value = None):
"""Create binary node."""
self.value = value
self.left = None
self.right = None
self.height = 0
def computeHeight(self):
"""Compute height of node in BST."""
height = -1
if (self.left):
height = max(height, self.left.height)
if (self.right):
height = max(height, self.right.height)
self.height = height + 1
def dynamicHeight(self):
"""Compute height of node in BST."""
height = -1
if (self.left):
height = max(height, self.left.dynamicHeight())
if (self.right):
height = max(height, self.right.dynamicHeight())
return height + 1
def dynamicHeightDifference(self):
"""Compute height difference of node's children in BST."""
leftTarget = 0
rightTarget = 0
if (self.left):
leftTarget = 1 + self.left.dynamicHeight()
if (self.right):
rightTarget = 1 + self.right.dynamicHeight()
return leftTarget - rightTarget
def heightDifference(self):
"""Compute height difference of node's children in BST."""
leftTarget = 0
rightTarget = 0
if (self.left):
leftTarget = 1 + self.left.height
if (self.right):
rightTarget = 1 + self.right.height
return leftTarget - rightTarget
def assertAVLProperty(self):
"""Validate AVL property for BST node."""
if abs(self.dynamicHeightDifference()) > 1:
return False
if (self.left):
if not self.left.assertAVLProperty():
return False
if (self.right):
if not self.right.assertAVLProperty():
return False
return True
def rotateRight(self):
"""Perform right rotation around given node."""
newRoot = self.left
grandson = newRoot.right
self.left = grandson
newRoot.right = self
self.computeHeight()
return newRoot
def rotateLeft(self):
"""Perform left rotation around given node."""
newRoot = self.right
grandson = newRoot.left
self.right = grandson
newRoot.left = self
self.computeHeight()
return newRoot
def rotateLeftRight(self):
"""Perform left, then right rotation around given node."""
child = self.left
newRoot = child.right
grand1 = newRoot.left
grand2 = newRoot.right
child.right = grand1
self.left = grand2
newRoot.left = child
newRoot.right = self
child.computeHeight()
self.computeHeight()
return newRoot
def rotateRightLeft(self):
"""Perform right, then left rotation around given node."""
child = self.right
newRoot = child.left
grand1 = newRoot.left
grand2 = newRoot.right
child.left = grand2
self.right = grand1
newRoot.left = self
newRoot.right = child
child.computeHeight()
self.computeHeight()
return newRoot
def add(self, val):
"""Adds a new node to the tree with value and Rebalance as needed."""
newRoot = self
if val <= self.value:
self.left = self.addToSubTree(self.left, val)
if self.heightDifference() == 2:
if val <= self.left.value:
newRoot = self.rotateRight()
else:
newRoot = self.rotateLeftRight()
else:
self.right = self.addToSubTree(self.right, val)
if self.heightDifference() == -2:
if val > self.right.value:
newRoot = self.rotateLeft()
else:
newRoot = self.rotateRightLeft()
newRoot.computeHeight()
return newRoot
def addToSubTree(self, parent, val):
"""Add val to parent subtree (if exists) and return root of that subtree."""
if parent is None:
return BinaryNode(val)
parent = parent.add(val)
return parent
def removeFromParent(self, parent, val):
"""Helper method for remove. Ensures proper behavior when removing node that
has children."""
if parent:
return parent.remove(val)
return None
def remove(self, val):
"""
Remove val of self from BinaryTree. Works in conjunction with remove
method in BinaryTree.
"""
newRoot = self
if self.value == val:
if self.left is None:
return self.right
child = self.left
while child.right:
child = child.right
childKey = child.value;
self.left = self.removeFromParent(self.left, childKey)
self.value = childKey;
if self.heightDifference() == -2:
if self.right.heightDifference() <= 0:
newRoot = self.rotateLeft()
else:
newRoot = self.rotateRightLeft()
elif self.value > val:
self.left = self.removeFromParent(self.left, val)
if self.heightDifference() == -2:
if self.right.heightDifference() <= 0:
newRoot = self.rotateLeft()
else:
newRoot = self.rotateRightLeft()
else:
self.right = self.removeFromParent(self.right, val)
if self.heightDifference() == 2:
if self.left.heightDifference() >= 0:
newRoot = self.rotateRight()
else:
newRoot = self.rotateLeftRight()
newRoot.computeHeight()
return newRoot
def __repr__(self):
"""Useful debugging function to produce linear tree representation."""
leftS = ''
rightS = ''
if self.left:
leftS = str(self.left)
if self.right:
rightS = str(self.right)
return "(L:" + leftS + " " + str(self.value) + " R:" + rightS + ")"
def inorder(self):
"""In order traversal generator of tree rooted at given node."""
if self.left:
for v in self.left.inorder():
yield v
yield self.value
if self.right:
for v in self.right.inorder():
yield v
class BinaryTree:
def __init__(self):
"""Create empty binary tree."""
self.root = None
def __str__(self):
if self.root:
return str(self.root)
def add(self, value):
"""Insert value into proper location in Binary Tree."""
if self.root is None:
self.root = BinaryNode(value)
else:
self.root = self.root.add(value)
def __contains__(self, target):
"""Check whether BST contains target value."""
node = self.root
while node:
if node.value == target:
return True
elif node.value > target:
node = node.left
else:
node = node.right
return False
def remove(self, val):
"""Remove value from tree."""
if self.root:
self.root = self.root.remove(val)
def __iter__(self):
"""In order traversal of elements in the tree."""
if self.root:
for e in self.root.inorder():
yield e
def assertAVLProperty(self):
"""Validate AVL property for BST Tree."""
if self.root:
return self.root.assertAVLProperty()
return True
def __repr__(self):
if self.root is None:
return "avl:()"
return "avl:" + str(self.root)