AVL Trees
AVL Trees
Sayan Sikder
SCOPE, VIT University, Vellore
2023
Motivation
So far …
– Binary search trees store linearly ordered data
– Best case height: O(log(n))
– Worst case height: O(n)
Requirement:
– Define and maintain a balance to ensure O(log(n))
height
Binary Search Tree review
• Suggestion 1::the left and right subtrees of root have the same
height
– Doesn’t force the tree to be shallow
• Suggestion 2: every node must have left and right subtrees of
the same height
– Only complete binary trees satisfy
– Too rigid to be useful
• Our choice: for each node, the height of the left and right
subtrees can differ at most 1.
• Balance factor: Left subtree-Right subtree = {-1,0,1} should
be maintained at each node.
AVL Trees
6 8
6
Insert 6 Restore AVL property
Original AVL tree
Property violated
Pros and Cons
Advantages
• The height of the AVL tree is always balanced. The height never grows beyond
log N, where N is the total number of nodes in the tree.
• It gives better search time complexity when compared to simple Binary
Search trees.
• AVL trees have self-balancing capabilities
Disadvantages