AVL
Trees
The biggest challenge in BST is,
What if the binary search tree insert element comes in a
sorted (Ascending or Descending) manner?
It looks like below:
From the above figure, it is observed that, BST’s worst case
performance is similar to linear search algorithms which is O(n). In real time,
we cannot predict the incoming of data; it may be of any order. To optimize the
search operation in BST, we need to balance out the existing BST.
The AVL trees are named after their inventors, Adelson, Velski & Landis. The
AVL trees are height balancing binary search tree. AVL trees checks the height
of the left and right subtrees, and assures that the difference is not more
than 1. This difference is called the Balance
Factor.
The following first tree is balanced and the other two trees are not balanced.
In the above example, the first tree is a balanced tree. The
height difference between the left and right subtree is 0. The second tree is a
not balanced tree, the left subtree of C has height 2, the right subtree of C
has height 0 and the difference between the left and right subtree is 2. AVL
tree permits difference (balance factor) to be only 1. The third tree also has
difference of 2.
Balance Factor = height (left subtree)
– height (right subtree)
If the difference in the height of left and right subtrees is more than 1, the tree is balanced using the rotating techniques.
AVL Rotations
To balance an unbalanced tree, we follow the four basic rotation rules such as:
The above first two rotation are single rotation and the next two rotations are double rotation. The unbalanced tree atleast have a balance factor of 2. With simple tree example, lets understand the above rotations one by one.
Left
Rotation
If a tree becomes unbalanced, when a node is inserted into the right subtree of the right subtree, then we perform single left rotation as shown below:
In the above example, the node A is unbalanced as the right subtree is loaded. To make this above tree balanced, we have to perform left rotation by making A the left subtree of B.
Right
Rotation
If a tree becomes unbalanced, when a node is inserted into the left subtree of the left subtree, then we perform single right rotation as shown below:
In the above example, the node C is unbalanced as the left subtree is loaded. To make the above tree balanced, we have to perform right rotation by making C as the right subtree of B.
Left-Right
Rotation
Double rotations are slightly complex version of already explained
versions of rotations. To understand this better, we need to perform certain
action while rotation. In left-right rotation, we perform left rotation and
followed by right rotation.
From the above figure,
Right-Left
rotation
The second type of double rotation is Right-Left rotation. It is a right rotation followed by left rotation.
From the above figure,
Time
Complexity in AVL trees
Most of the BST operations such as insertion, search, delete, max , min, etc take O(h) time where h is the height of the BST. In the worst case, the skewed trees takes O(n) time complexity. If we make sure that the height of the tree remains O(Log n) after every insertion and deletion, then it is sure that an upper bound of O(Log n) for all these operations. The height of the AVL tree is always O(Log n) where n is the number of nodes in the tree.