-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAVLTree.py
More file actions
executable file
·375 lines (311 loc) · 13.4 KB
/
AVLTree.py
File metadata and controls
executable file
·375 lines (311 loc) · 13.4 KB
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# This code is adopted from Zybooks.com
# CS lab 3A
# Govinda KC
# -*- coding: utf-8 -*-
class Node:
# Constructor with a key parameter creates the Node object.
def __init__(self, key, embedding):
self.key = key
self.embedding = embedding
self.parent = None
self.left = None
self.right = None
self.height = 0
# Method to calculate the current nodes's balance factor node,
# defined as height(left subtree) - height(right subtree)
def get_balance(self):
# Get current height of left subtree, or -1 if None
left_height = -1
if self.left is not None:
left_height = self.left.height
# Get right subtree's current height, or -1 if None
right_height = -1
if self.right is not None:
right_height = self.right.height
# Calculate the balance factor.
return left_height - right_height
# Recalculates the current height of the subtree rooted at
# the node, usually called after a subtree has been
# modified.
def update_height(self):
# Get current height of left subtree, or -1 if None
left_height = -1
if self.left is not None:
left_height = self.left.height
# Get current height of right subtree, or -1 if None
right_height = -1
if self.right is not None:
right_height = self.right.height
# Assign self.height with calculated node height.
self.height = max(left_height, right_height) + 1
# Assign either the left or right data member with a new
# child. The parameter which_child is expected to be the
# string "left" or the string "right". Returns True if
# the new child is successfully assigned to this node, False
# otherwise.
def set_child(self, which_child, child):
# Ensure which_child is properly assigned.
if which_child != "left" and which_child != "right":
return False
# Assign the left or right data member.
if which_child == "left":
self.left = child
else:
self.right = child
# Assign the new child's parent data member,
# if the child is not None.
if child is not None:
child.parent = self
# Update the node's height, since the structure
# of the subtree may have changed.
self.update_height()
return True
# Replace a current child with a new child. Determines if
# the current child is on the left or right, and calls
# set_child() with the new node appropriately.
# Returns True if the new child is assigned, False otherwise.
def replace_child(self, current_child, new_child):
if self.left is current_child:
return self.set_child("left", new_child)
elif self.right is current_child:
return self.set_child("right", new_child)
# If neither of the above cases applied, then the new child
# could not be attached to this node.
return False
def get_embedding(self):
if self.embedding is not None:
return self.embedding
def set_embedding(self, array):
self.embedding = array
class AVLTree:
def __init__(self):
# Constructor to create an empty AVLTree. There is only
# one data member, the tree's root Node, and it starts
# out as None.
self.root = None
# Performs a left rotation at the given node. Returns the
# subtree's new root.
def rotate_left(self, node):
# Define a convenience pointer to the right child of the
# left child.
right_left_child = node.right.left
# Step 1 - the right child moves up to the node's position.
# This detaches node from the tree, but it will be reattached
# later.
if node.parent is not None:
node.parent.replace_child(node, node.right)
else: # node is root
self.root = node.right
self.root.parent = None
# Step 2 - the node becomes the left child of what used
# to be its right child, but is now its parent. This will
# detach right_left_child from the tree.
node.right.set_child('left', node)
# Step 3 - reattach right_left_child as the right child of node.
node.set_child('right', right_left_child)
return node.parent
# Performs a right rotation at the given node. Returns the
# subtree's new root.
def rotate_right(self, node):
# Define a convenience pointer to the left child of the
# right child.
left_right_child = node.left.right
# Step 1 - the left child moves up to the node's position.
# This detaches node from the tree, but it will be reattached
# later.
if node.parent is not None:
node.parent.replace_child(node, node.left)
else: # node is root
self.root = node.left
self.root.parent = None
# Step 2 - the node becomes the right child of what used
# to be its left child, but is now its parent. This will
# detach left_right_child from the tree.
node.left.set_child('right', node)
# Step 3 - reattach left_right_child as the left child of node.
node.set_child('left', left_right_child)
return node.parent
# Updates the given node's height and rebalances the subtree if
# the balancing factor is now -2 or +2. Rebalancing is done by
# performing a rotation. Returns the subtree's new root if
# a rotation occurred, or the node if no rebalancing was required.
def rebalance(self, node):
# First update the height of this node.
node.update_height()
# Check for an imbalance.
if node.get_balance() == -2:
# The subtree is too big to the right.
if node.right.get_balance() == 1:
# Double rotation case. First do a right rotation
# on the right child.
self.rotate_right(node.right)
# A left rotation will now make the subtree balanced.
return self.rotate_left(node)
elif node.get_balance() == 2:
# The subtree is too big to the left
if node.left.get_balance() == -1:
# Double rotation case. First do a left rotation
# on the left child.
self.rotate_left(node.left)
# A right rotation will now make the subtree balanced.
return self.rotate_right(node)
# No imbalance, so just return the original node.
return node
# Insert a new node into the AVLTree. When insert() is complete,
# the AVL tree will be balanced.
def insert(self, node):
# Special case: if the tree is empty, just set the root to
# the new node.
if self.root is None:
self.root = node
node.parent = None
else:
# Step 1 - do a regular binary search tree insert.
current_node = self.root
while current_node is not None:
# Choose to go left or right
if node.key < current_node.key:
# Go left. If left child is None, insert the new
# node here.
if current_node.left is None:
current_node.left = node
node.parent = current_node
current_node = None
else:
# Go left and do the loop again.
current_node = current_node.left
else:
# Go right. If the right child is None, insert the
# new node here.
if current_node.right is None:
current_node.right = node
node.parent = current_node
current_node = None
else:
# Go right and do the loop again.
current_node = current_node.right
# Step 2 - Rebalance along a path from the new node's parent up
# to the root.
node = node.parent
while node is not None:
self.rebalance(node)
node = node.parent
# Writes nodes k distance from root to file
def _depth(self, k):
_dep = self._depth_total(self.root, k)
f=open("AVL_depth.txt", "a+")
for i in range (len(_dep)):
f.write(str(_dep[i]+" \n"))
f.close()
return None
def _depth_total(self, node, k):
arr = []
if node is None:
return
if k==0:
arr.append(node.key)
else:
arr = arr + self._depth_total(node.left, k-1)
arr = arr + self._depth_total(node.right, k-1)
return arr
# Writes tree in ascending order to file
def _write(self):
_ascend = self._write_afile(self.root)
f=open("AVL_tree.txt", "a+", encoding="utf-8")
for i in range (len(_ascend)):
f.write(str(_ascend[i])+" \n")
f.close()
return None
def _write_afile(self, node):
arr = []
if node:
arr = self._write_afile(node.left)
arr.append(node.key)
arr = arr + self._write_afile(node.right)
return arr
# Returns the height of this tree
def _height(self):
return self._height_total(self.root)
def _height_total(self, node):
if node is None:
return -1
left_height = self._height_total(node.left)
right_height = self._height_total(node.right)
return 1 + max(left_height, right_height)
# Recursively compute the number of nodes AKA the size of this tree
def _size(self):
return self._size_total(self.root)
def _size_total(self, node):
if node is None:
return 0
else:
return self._size_total(node.left) + 1 + self._size_total(node.right)
# Searches for a node with a matching key. Does a regular
# binary search tree search operation. Returns the node with the
# matching key if it exists in the tree, or None if there is no
# matching key in the tree.
def search(self, key):
current_node = self.root
while current_node is not None:
# Compare the current node's key with the target key.
# If it is a match, return the current key; otherwise go
# either to the left or right, depending on whether the
# current node's key is smaller or larger than the target key.
if current_node.key == key: return current_node
elif current_node.key < key: current_node = current_node.right
else: current_node = current_node.left
return None
# Attempts to remove a node with a matching key. If no node has a matching key
# then nothing is done and False is returned; otherwise the node is removed and
# True is returned.
def remove_key(self, key):
node = self.search(key)
if node is None:
return False
else:
return self.remove_node(node)
# Removes the given node from the tree. The left and right subtrees,
# if they exist, will be reattached to the tree such that no imbalances
# exist, and the binary search tree property is maintained. Returns True
# if the node is found and removed, or False if the node is not found in
# the tree.es return after directly removing the node.
def remove_node(self, node):
# Base case:
if node is None:
return False
# Parent needed for rebalancing.
parent = node.parent
# Case 1: Internal node with 2 children
if node.left is not None and node.right is not None:
# Find successor
successor_node = node.right
while successor_node.left != None:
successor_node = successor_node.left
# Copy the value from the node
node.key = successor_node.key
# Recursively remove successor
self.remove_node(successor_node)
# Nothing left to do since the recursive call will have rebalanced
return True
# Case 2: Root node (with 1 or 0 children)
elif node is self.root:
if node.left is not None:
self.root = node.left
else:
self.root = node.right
if self.root is not None:
self.root.parent = None
return True
# Case 3: Internal with left child only
elif node.left is not None:
parent.replace_child(node, node.left)
# Case 4: Internal with right child only OR leaf
else:
parent.replace_child(node, node.right)
# node is gone. Anything that was below node that has persisted is already correctly
# balanced, but ancestors of node may need rebalancing.
node = parent
while node is not None:
self.rebalance(node)
node = node.parent
return True