-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathavl_tree.py
195 lines (144 loc) · 5.51 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
# derived from https://favtutor.com/blogs/avl-tree-python with permission
from collections import deque
class Node:
def __init__(self, key):
self.key = key
self.left = None
self.right = None
self.height = 1
class AVLTree:
def __init__(self):
self.root = None
def get_height(self, node):
return 0 if not node else node.height
def get_balance_factor(self, node):
return 0 if not node else (self.get_height(node.left) - self.get_height(node.right))
def get_min_node(self, node):
return node if not node or not node.left else self.get_min_node(node.left)
# O(logn)
def search(self, key):
x = self.root
while x is not None and key != x.key:
if key < x.key:
x = x.left
else:
x = x.right
return x
# O(logn)
def insert(self, root, key):
if not root:
return Node(key)
elif key < root.key:
root.left = self.insert(root.left, key)
else:
root.right = self.insert(root.right, key)
root.height = 1 + max(self.get_height(root.left), self.get_height(root.right))
# Update the balance factor and balance the tree
bf = self.get_balance_factor(root)
if bf > 1 and key < root.left.key:
return self.right_rotate(root)
if bf < -1 and key > root.right.key:
return self.left_rotate(root)
if bf > 1 and key > root.left.key:
root.left = self.left_rotate(root.left)
return self.right_rotate(root)
if bf < -1 and key < root.right.key:
root.right = self.right_rotate(root.right)
return self.left_rotate(root)
return root
# O(logn)
def delete(self, root, key):
if not root:
return root
elif key < root.key:
root.left = self.delete(root.left, key)
elif key > root.key:
root.right = self.delete(root.right, key)
else:
if not root.left:
temp = root.right
root = None
return temp
elif not root.right:
temp = root.left
root = None
return temp
# find inorder successor
temp = self.get_min_node(root.right)
root.key = temp.key
root.right = self.delete(root.right, temp.key)
root.height = 1 + max(self.get_height(root.left), self.get_height(root.right))
# Update the balance factor and balance the tree
bf = self.get_balance_factor(root)
if bf > 1 and self.get_balance_factor(root.left) >= 0:
return self.right_rotate(root)
if bf < -1 and self.get_balance_factor(root.right) <= 0:
return self.left_rotate(root)
if bf > 1 and self.get_balance_factor(root.left) < 0:
root.left = self.left_rotate(root.left)
return self.right_rotate(root)
if bf < -1 and self.get_balance_factor(root.right) > 0:
root.right = self.right_rotate(root.right)
return self.left_rotate(root)
return root
# O(1)
def left_rotate(self, node):
B = node.right
Y = B.left
B.left = node
node.right = Y
node.height = 1 + max(self.get_height(node.left),
self.get_height(node.right))
B.height = 1 + max(self.get_height(B.left),
self.get_height(B.right))
return B
# O(1)
def right_rotate(self, node):
A = node.left
Y = A.right
A.right = node
node.left = Y
node.height = 1 + max(self.get_height(node.left), self.get_height(node.right))
A.height = 1 + max(self.get_height(A.left), self.get_height(A.right))
return A
# Level-order tree traversal
def print_tree(self):
if self.root:
queue = deque()
queue.append(self.root)
level_order = ''
level_order_with_details = ''
while(queue):
node = queue.popleft()
level_order += f'{node.key} '
level_order_with_details += f'{node.key}: '.ljust(5) + f'h = {self.get_height(node)}, bf = {self.get_balance_factor(node)}\n'
# add children to queue
if node.left != None:
queue.append(node.left)
if node.right != None:
queue.append(node.right)
print('\nLevel-order traversal:')
print(level_order)
print(f'\nLevel-order traversal with height and balance factor:')
print(level_order_with_details)
else:
print('\nAVL tree is empty!')
def print_search_result(result):
return result.key if result else 'not found'
def main():
avl = AVLTree()
keys = [50, 25, 75, 15, 35, 60, 120, 10, 68, 90, 125, 83, 100]
for key in keys:
avl.root = avl.insert(avl.root, key)
avl.print_tree()
result = avl.search(125)
print(f'Search for 125: {print_search_result(result)}')
result = avl.search(1)
print(f'Search for 1: {print_search_result(result)}')
avl.root = avl.delete(avl.root, 120)
print('\nAfter deleting 120:')
avl.print_tree()
avl.root = avl.delete(avl.root, 10)
print('After deleting 10:')
avl.print_tree()
main()