-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathavl_tree.py
200 lines (155 loc) · 6.13 KB
/
avl_tree.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
""" AVL Tree Implementation in Python
AVL tree is a self-balancing Binary Search Tree (BST) where the difference
between heights of left and right subtrees cannot be more than one for
all nodes.
"""
class Node: # pylint: disable=too-few-public-methods
""" Node contains actual data and address to left and right node """
def __init__(self, data):
self.data = data
self.left = None
self.right = None
self.height = 1
class AVLTree:
""" AVL Tree Implementation """
def __init__(self):
self.root = None
def insert(self, key):
""" inserts new integer data into the tree at the position, so that
the AVL tree property is maintained.
"""
def _insert(root, key):
""" recursive internal method which works on node level """
if root is None:
return Node(key)
if key < root.data:
root.left = _insert(root.left, key)
else:
root.right = _insert(root.right, key)
root.height = max(self.get_height(root.left),
self.get_height(root.right)) + 1
balance_factor = self.get_balance_factor(root)
# if node is unbalanced
# case 1 - left left
if balance_factor > 1 and key < root.left.data:
return self.right_rotate(root)
# case 2 - right right
if balance_factor < -1 and key > root.right.data:
return self.left_rotate(root)
# case 3 - left right
if balance_factor > 1 and key > root.left.data:
root.left = self.left_rotate(root.left)
return self.right_rotate(root)
# case 4 - right left
if balance_factor < -1 and key < root.right.data:
root.right = self.right_rotate(root.right)
return self.left_rotate(root)
return root
self.root = _insert(self.root, key)
def delete(self, key):
""" delete value from AVL tree """
def find_min_node(node):
""" find the node with minimum value of give (sub)tree """
while node.left is not None:
node = node.left
return node
def _delete(root, key):
""" interal recursive method that works at node-level """
if root is None:
return root
if key < root.data:
root.left = _delete(root.left, key)
elif key > root.data:
root.right = _delete(root.right, key)
else:
if root.left is None and root.right is None:
del root
root = None
elif root.left is None:
temp = root
root = root.right
del temp
elif root.right is None:
temp = root
root = root.left
del temp
else:
temp = find_min_node(root.right)
root.data = temp.data
root.right = _delete(root.right, temp.data)
root.height = max(self.get_height(root.left),
self.get_height(root.right)) + 1
balance_factor = self.get_balance_factor(root)
# if node is unbalanced
# case 1 - left left
if balance_factor > 1 and self.get_balance_factor(root.left) >= 0:
return self.right_rotate(root)
# case 2 - right right
if balance_factor < -1 and self.get_balance_factor(root.right) <= 0:
return self.left_rotate(root)
# case 3 - left right
if balance_factor > 1 and self.get_balance_factor(root.left) < 0:
root.left = self.left_rotate(root.left)
return self.right_rotate(root)
# case 4 - right left
if balance_factor < -1 and self.get_balance_factor(root.right) > 0:
root.right = self.right_rotate(root.right)
return self.left_rotate(root)
return root
self.root = _delete(self.root, key)
def right_rotate(self, node):
""" perform right rotation on given node """
left_node = node.left
subtree = left_node.right
# perform rotation
left_node.right = node
node.left = subtree
node.height = max(self.get_height(node.left),
self.get_height(node.right)) + 1
left_node.height = max(self.get_height(left_node.left),
self.get_height(left_node.right)) + 1
return left_node
def left_rotate(self, node):
""" perform left rotation on given node """
right_node = node.right
subtree = right_node.left
# perform rotation
right_node.left = node
node.right = subtree
node.height = max(self.get_height(node.left),
self.get_height(node.right)) + 1
right_node.height = max(self.get_height(right_node.left),
self.get_height(right_node.right)) + 1
return right_node
@staticmethod
def get_height(node):
""" returns height of node """
if node is None:
return 0
return node.height
def get_balance_factor(self, node):
""" returns balance factor at node """
if node is None:
return 0
return self.get_height(node.left) - self.get_height(node.right)
def pre_order(node):
""" prints pre order traversal """
if node is None:
return
print(node.data, end=" ")
pre_order(node.left)
pre_order(node.right)
def main():
""" operational function """
avl_tree = AVLTree()
for elem in [9, 5, 10, 0, 6, 11, -1, 1, 2]:
avl_tree.insert(elem)
print("preorder traversal after insertion")
pre_order(avl_tree.root) # 9 1 0 -1 5 2 6 10 11
print()
avl_tree.delete(10)
print("preorder traversal after deletion")
pre_order(avl_tree.root) # 1 0 -1 9 5 2 6 11
print()
if __name__ == "__main__":
main()