-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAVLTree.py
More file actions
217 lines (182 loc) · 8.07 KB
/
AVLTree.py
File metadata and controls
217 lines (182 loc) · 8.07 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Nov 4 20:43:09 2018
@author: JesusMHernandez
"""
import AVLNode as AVLNode
class AVLTree:
# Constructor to create an empty AVLTree. There is only
# one data member, the tree's root Node, and it starts
# out as None.
def __init__(self):
self.root = None
# Performs a left rotation at the given node. Returns the
# new root of the subtree.
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
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
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
# 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)