Repository navigation
Expand file tree
/
Copy pathAVL_Tree.py
More file actions
175 lines (141 loc) · 5.42 KB
/
Copy pathAVL_Tree.py
File metadata and controls
175 lines (141 loc) · 5.42 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
# AVL Trees
class AVLNode:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
self.height = 1
"""
The height of a node is the maximum of the height of the left child and the height of the right child, plus 1.
"""
class AVLTree:
def __init__(self):
self.root = None
def getHeight(self, node):
if not node:
return 0
return node.height
def getBalance(self, node):
if not node:
return 0
return self.getHeight(node.left) - self.getHeight(node.right)
def inorder_search(self, root):
if root:
self.inorder_search(root.left)
print(root.data, end=' ')
self.inorder_search(root.right)
def rotateLeft(self, node):
rightChild = node.right
leftChild = rightChild.left
rightChild.left = node
node.right = leftChild
node.height = 1 + max(self.getHeight(node.left), self.getHeight(node.right))
rightChild.height = 1 + max(self.getHeight(rightChild.left), self.getHeight(rightChild.right))
"""
The height of a node is the maximum of the height of the left child and the height of the right child, plus 1.
"""
return rightChild
def rotateRight(self, node):
leftChild = node.left
rightChild = leftChild.right
leftChild.right = node
node.left = rightChild
node.height = 1 + max(self.getHeight(node.left), self.getHeight(node.right))
leftChild.height = 1 + max(self.getHeight(leftChild.left), self.getHeight(leftChild.right))
return leftChild
def insert(self, root, data):
if not root:
return AVLNode(data)
elif data < root.data:
root.left = self.insert(root.left, data)
else:
root.right = self.insert(root.right, data)
# Update the height of the root
root.height = 1 + max(self.getHeight(root.left), self.getHeight(root.right))
# Get the balance factor of the root
balance = self.getBalance(root)
# If the root is unbalanced, then there are 4 cases
# 1. Left-Left Case
if balance > 1 and data < root.left.data:
return self.rotateRight(root)
# 2. Right-Right Case
if balance < -1 and data > root.right.data:
return self.rotateLeft(root)
# 3. Left-Right Case
if balance > 1 and data > root.left.data:
root.left = self.rotateLeft(root.left)
return self.rotateRight(root)
# 4. Right-Left Case
if balance < -1 and data < root.right.data:
root.right = self.rotateRight(root.right)
return self.rotateLeft(root)
return root
def delete(self, root, data):
if not root:
return root
elif data < root.data:
root.left = self.delete(root.left, data)
elif data > root.data:
root.right = self.delete(root.right, data)
else:
if root.left is None:
temp = root.right
root = None
return temp
elif root.right is None:
temp = root.left
root = None
return temp
temp = self.getMinValueNode(root.right)
root.data = temp.data
root.right = self.delete(root.right, temp.data)
if root is None:
return root
# Update the height of the root
root.height = 1 + max(self.getHeight(root.left), self.getHeight(root.right))
# Get the balance factor of the root
balance = self.getBalance(root)
# If the root is unbalanced, then there are 4 cases
# 1. Left-Left Case
if balance > 1 and self.getBalance(root.left) >= 0:
return self.rotateRight(root)
# 2. Right-Right Case
if balance < -1 and self.getBalance(root.right) <= 0:
return self.rotateLeft(root)
# 3. Left-Right Case
if balance > 1 and self.getBalance(root.left) < 0:
root.left = self.rotateLeft(root.left)
return self.rotateRight(root)
# 4. Right-Left Case
if balance < -1 and self.getBalance(root.right) > 0:
root.right = self.rotateRight(root.right)
return self.rotateLeft(root)
return root
def getMinValueNode(self, root):
if root is None or root.left is None:
return root
return self.getMinValueNode(root.left)
def search(self, root, data):
if root is None or root.data == data:
return root
if root.data < data:
return self.search(root.right, data)
return self.search(root.left, data)
def getMinValue(self, root):
current = root
while current.left is not None:
current = current.left
return current
# Driver code
avl = AVLTree()
values = [9, 5, 10, 0, 6, 11, -1, 1, 2]
for value in values:
avl.root = avl.insert(avl.root, value)
avl.inorder_search(avl.root)
print()
avl.root = avl.delete(avl.root, 10)
avl.inorder_search(avl.root)
print()
avl.root = avl.delete(avl.root, 1)
avl.inorder_search(avl.root)
print()