-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAVL Tree.py
More file actions
167 lines (128 loc) · 4.83 KB
/
AVL Tree.py
File metadata and controls
167 lines (128 loc) · 4.83 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
class Node:
def __init__(self, key):
self.key = key
self.left = None
self.right = None
self.height = 1
class AVLTree:
def get_height(self, root):
if not root:
return 0
return root.height
def get_balance(self, root):
if not root:
return 0
return self.get_height(root.left) - self.get_height(root.right)
def rotate_right(self, y):
x = y.left
T2 = x.right
x.right = y
y.left = T2
y.height = 1 + max(self.get_height(y.left), self.get_height(y.right))
x.height = 1 + max(self.get_height(x.left), self.get_height(x.right))
return x
def rotate_left(self, x):
y = x.right
T2 = y.left
y.left = x
x.right = T2
x.height = 1 + max(self.get_height(x.left), self.get_height(x.right))
y.height = 1 + max(self.get_height(y.left), self.get_height(y.right))
return y
def insert(self, root, key):
if not root:
return Node(key)
if key < root.key:
root.left = self.insert(root.left, key)
elif key > root.key:
root.right = self.insert(root.right, key)
else:
return root
root.height = 1 + max(self.get_height(root.left), self.get_height(root.right))
balance = self.get_balance(root)
if balance > 1 and key < root.left.key:
return self.rotate_right(root)
if balance < -1 and key > root.right.key:
return self.rotate_left(root)
if balance > 1 and key > root.left.key:
root.left = self.rotate_left(root.left)
return self.rotate_right(root)
if balance < -1 and key < root.right.key:
root.right = self.rotate_right(root.right)
return self.rotate_left(root)
return root
def delete(self, root, key):
if not root:
return root
if 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:
return root.right
elif not root.right:
return root.left
temp = self.get_min_value_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))
balance = self.get_balance(root)
if balance > 1 and self.get_balance(root.left) >= 0:
return self.rotate_right(root)
if balance > 1 and self.get_balance(root.left) < 0:
root.left = self.rotate_left(root.left)
return self.rotate_right(root)
if balance < -1 and self.get_balance(root.right) <= 0:
return self.rotate_left(root)
if balance < -1 and self.get_balance(root.right) > 0:
root.right = self.rotate_right(root.right)
return self.rotate_left(root)
return root
def get_min_value_node(self, root):
if root is None or root.left is None:
return root
return self.get_min_value_node(root.left)
def search(self, root, key):
if root is None or root.key == key:
return root
if key < root.key:
return self.search(root.left, key)
return self.search(root.right, key)
def inorder_traversal(self, root):
if not root:
return []
return self.inorder_traversal(root.left) + [root.key] + self.inorder_traversal(root.right)
def main():
avl_tree = AVLTree()
root = None
while True:
print("\nOptions:")
print("1. Insert")
print("2. Delete")
print("3. Search")
print("4. Traverse (Inorder)")
print("5. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
key = int(input("Enter key to insert: "))
root = avl_tree.insert(root, key)
elif choice == 2:
key = int(input("Enter key to delete: "))
root = avl_tree.delete(root, key)
elif choice == 3:
key = int(input("Enter key to search: "))
result = avl_tree.search(root, key)
if result:
print(f"Key {key} found in the tree.")
else:
print(f"Key {key} not found in the tree.")
elif choice == 4:
print("Inorder Traversal:", avl_tree.inorder_traversal(root))
elif choice == 5:
print("Exiting...")
break
else:
print("Invalid choice! Please try again.")
if __name__ == "__main__":
main()