Searching operation is similar to as that of Bst but searching in AVL
is more efficient than BST as AVL is self balanced BST.
In BST, the time complexity of search operation (average case) is taken to be O(log n). But in the worst case, i.e the degenerate trees/skewed trees time complexity of search operation is O(n) which can also be attained using array or linked list. So, in order to remove this problem, balanced binary search tree (AVL tree) was introduced in which the time complexity of search operation remains O(log n).
Program:
classTreeNode(object):def__init__(self,_val):
self.val = _val
self.left = None
self.right = None
self.height = 1classAVL_Tree(object):#steps:# 1)perform simple BST insertion# 2)modify the height # 3)Get the Balancing Factor# 4)Balance The tree using required set of rotation to get balanced binary search tree,ie. AVLdefinsert(self, root, val):#Simple Bst Insertion:ifnot root:
return TreeNode(val)
elif val < root.val:
root.left = self.insert(root.left, val)
else:
root.right = self.insert(root.right, val)
# 2)modify the height
root.height = 1 + max(self.Height(root.left), self.Height(root.right))
# 3)Get the Balancing Factor
balance = self.check_Avl(root)
# 4)Balance The tree using required set of rotation#RR Rotation as tree is Left Skewedif balance > 1and val < root.left.val:
return self.RR(root)
#LL Rotation as tree is Right Skewedif balance < -1and val > root.right.val:
return self.LL(root)
#RL Rotation as tree is Left then Right Skewedif balance > 1and val > root.left.val:
root.left = self.LL(root.left)
return self.RR(root)
#LR Rotation as tree is Right then Left Skewedif balance < -1and val < root.right.val:
root.right = self.RR(root.right)
return self.LL(root)
return root
#LL RotationdefLL(self, node):
p = node.right
t = p.left
#Rotations:
p.left = node
node.right = t
#modify the heights:
node.height = 1 + max(self.Height(node.left), self.Height(node.right))
p.height = 1 + max(self.Height(p.left), self.Height(p.right))
return p
#LL RotationdefRR(self, node):
p = node.left
t = p.right
#Rotations:
p.right = node
node.left = t
#modify the heights:
node.height = 1 + max(self.Height(node.left), self.Height(node.right))
p.height = 1 + max(self.Height(p.left), self.Height(p.right))
return p
#Getting the HeightdefHeight(self, root):ifnot root:
return0return root.height
#Getting the Balancing Factordefcheck_Avl(self, root):ifnot root:
return0return self.Height(root.left) - self.Height(root.right)
defpreOrder(self, root):ifnot root:
return
print("{0} ".format(root.val), end="")
self.preOrder(root.left)
self.preOrder(root.right)
definsert_data(_data):
mytree = AVL_Tree()
root = Nonefor i in _data:
root = mytree.insert(root,i)
print("Preorder Traversal of constructed AVL tree is:")
mytree.preOrder(root)
print()
return root
defSearch(root,val):if (root isNone):
returnFalseelif (root.val == val):
returnTrueelif(root.val < val):
return Search(root.right,val)
return Search(root.left,val)
returnFalsedefmain():
root = insert_data([10,8,15,7,9,12,17,16,18,6,4])
t = int(input('Enter the key to be searched:\t'))
if(Search(root,t)):
print()
print('"Element found in AVL Tree"')
else:
print()
print('"Element not found in AVL Tree"')
if __name__ == "__main__":
main()
else:
pass
Output:
Preorder Traversal of constructed AVL tree is:
10 8 6 4 7 9 15 12 17 16 18
Enter the key to be searched: 23
"Element not found in AVL Tree"
Comments