Skip to content

Commit 4811c39

Browse files
Create avltree.py
1 parent 432a4de commit 4811c39

File tree

1 file changed

+195
-0
lines changed

1 file changed

+195
-0
lines changed

avltree.py

+195
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
from __future__ import print_function
2+
#creating avl tree node
3+
4+
class treenode(object):
5+
def __init__(self,key):
6+
self.key=key
7+
self.left=None
8+
self.right=None
9+
self.height=1
10+
11+
#creating avl tree class
12+
class avltree(object):
13+
def get_height(self,root):
14+
if not root:
15+
return 0
16+
return root.height
17+
18+
def get_balance(self,root):
19+
if not root:
20+
return 0
21+
return self.get_height(root.left)-self.get_height(root.right)
22+
23+
def insert(self,root,key):
24+
#perform normal bst
25+
if not root:
26+
return treenode(key)
27+
elif key<root.key:
28+
root.left=self.insert(root.left,key)
29+
else:
30+
root.right=self.insert(root.right,key)
31+
#step 2- update the height of the ancestor node
32+
root.height=1+max(self.get_height(root.left),self.get_height(root.right))
33+
#3.get the balance factor
34+
balance=self.get_balance(root)
35+
#4.if the node is unbalanced ,then try out 4 cases
36+
#LL
37+
if balance >1 and key < root.left.key:
38+
return self.righrrotate(root)
39+
#RR
40+
if balance < -1 and key>root.right.key:
41+
return self.leftrotate(root)
42+
#LR
43+
if balance >1 and key > root.left.key:
44+
return self.righrrotate(root)
45+
#RL
46+
if balance <-1 and key<root.right.key:
47+
return self.leftrotate(root)
48+
return root
49+
50+
# Recursive function to delete a node with
51+
# given key from subtree with given root.
52+
# It returns root of the modified subtree.
53+
def delete(self, root, key):
54+
55+
# Step 1 - Perform standard BST delete
56+
if not root:
57+
return root
58+
59+
elif key < root.key:
60+
root.left = self.delete(root.left, key)
61+
62+
elif key > root.key:
63+
root.right = self.delete(root.right, key)
64+
65+
else:
66+
if root.left is None:
67+
temp = root.right
68+
root = None
69+
return temp
70+
71+
elif root.right is None:
72+
temp = root.left
73+
root = None
74+
return temp
75+
76+
temp = self.getMinValueNode(root.right)
77+
root.key = temp.key
78+
root.right = self.delete(root.right,
79+
temp.key)
80+
81+
# If the tree has only one node,
82+
# simply return it
83+
if root is None:
84+
return root
85+
86+
# Step 2 - Update the height of the
87+
# ancestor node
88+
root.height = 1 + max(self.get_height(root.left),
89+
self.get_height(root.right))
90+
91+
# Step 3 - Get the balance factor
92+
balance = self.get_balance(root)
93+
94+
# Step 4 - If the node is unbalanced,
95+
# then try out the 4 cases
96+
# Case 1 - Left Left
97+
if balance > 1 and self.get_balance(root.left) >= 0:
98+
return self.righrrotate(root)
99+
100+
# Case 2 - Right Right
101+
if balance < -1 and self.get_balance(root.right) <= 0:
102+
return self.leftrotate(root)
103+
104+
# Case 3 - Left Right
105+
if balance > 1 and self.get_balance(root.left) < 0:
106+
root.left = self.leftRotate(root.left)
107+
return self.righrrotate(root)
108+
109+
# Case 4 - Right Left
110+
if balance < -1 and self.get_balance(root.right) > 0:
111+
root.right = self.rightRotate(root.right)
112+
return self.leftrotate(root)
113+
114+
return root
115+
116+
def leftrotate(self,z):
117+
y=z.right
118+
t2=y.left
119+
#perform rotation
120+
y.left=z
121+
z.right=t2
122+
#upadate heights
123+
z.height=1+max(self.get_height(z.left),self.get_height(z.right))
124+
y.height=1+max(self.get_height(y.left),self.get_height(y.right))
125+
return y
126+
127+
def righrrotate(self,z):
128+
y=z.left
129+
t2=y.right
130+
#perform rotation
131+
y.right=z
132+
z.left=t2
133+
#upadate heights
134+
z.height=1+max(self.get_height(z.left),self.get_height(z.right))
135+
y.height=1+max(self.get_height(y.left),self.get_height(y.right))
136+
return y
137+
def preOrder(self, root):
138+
139+
if not root:
140+
return
141+
142+
print("{0} ".format(root.key), end="")
143+
self.preOrder(root.left)
144+
self.preOrder(root.right)
145+
def getMinValueNode(self, root):
146+
if root is None or root.left is None:
147+
return root
148+
149+
return self.getMinValueNode(root.left)
150+
151+
152+
153+
154+
root=None
155+
myroot=avltree()
156+
root=myroot.insert(root,8)
157+
root=myroot.insert(root,10)
158+
root=myroot.insert(root,1)
159+
root=myroot.insert(root,6)
160+
root=myroot.insert(root,0)
161+
root=myroot.insert(root,11)
162+
# Preorder Traversal
163+
print("Preorder traversal of the",
164+
"constructed AVL tree is")
165+
myroot.preOrder(root)
166+
print()
167+
root=myroot.insert(root,9)
168+
print("Preorder traversal of the",
169+
"constructed AVL tree is")
170+
myroot.preOrder(root)
171+
print()
172+
root=myroot.insert(root,14)
173+
print("Preorder traversal of the",
174+
"constructed AVL tree is")
175+
myroot.preOrder(root)
176+
print()
177+
178+
myTree =avltree()
179+
root = None
180+
nums = [9, 5, 10, 0, 6, 11, -1, 1, 2]
181+
182+
for num in nums:
183+
root = myTree.insert(root, num)
184+
print("Preorder traversal of the",
185+
"constructed AVL my tree is")
186+
myTree.preOrder(root)
187+
print()
188+
#Delete
189+
key = 10
190+
root = myTree.delete(root, key)
191+
192+
# Preorder Traversal
193+
print("Preorder Traversal after deletion -")
194+
myTree.preOrder(root)
195+
print()

0 commit comments

Comments
 (0)