AVL Tree
AVL Tree
AVL Tree
• AVL Tree is invented by GM Adelson - Velsky and EM Landis in 1962. The tree is named
AVL in honour of its inventors.
• AVL Tree can be defined as height balanced binary search tree in which each node is
associated with a balance factor which is calculated by subtracting the height of its right
sub-tree from that of its left sub-tree.
• Tree is said to be balanced if balance factor of each node is in between -1 to 1, otherwise,
the tree will be unbalanced and need to be balanced.
• Balance Factor (k) = height (left(k)) - height (right(k))
• If balance factor of any node is 1, it means that the left sub-tree is one level higher than
the right sub-tree.
• If balance factor of any node is 0, it means that the left sub-tree and right sub-tree contain
equal height.
• If balance factor of any node is -1, it means that the left sub-tree is one level lower than
the right sub-tree.
AVL tree
Why AVL Tree?
• AVL tree controls the height of the binary search tree by not letting it
to be skewed.
• The time taken for all operations in a binary search tree of height h
is O(h).
• However, it can be extended to O(n) if the BST becomes skewed (i.e.
worst case). By limiting this height to log n, AVL tree imposes an
upper bound on each operation to be O(log n) where n is the number
of nodes.
Insertion
• Insertion in AVL tree is performed in the same way as it is performed
in a binary search tree. However, it may lead to violation in the AVL
tree property and therefore the tree may need balancing. The tree
can be balanced by applying rotations.
Deletion
• Deletion can also be performed in the same way as it is performed in
a binary search tree. Deletion may also disturb the balance of the tree
therefore, various types of rotations are used to rebalance the tree.
AVL Rotations
• Double rotations are bit tougher than single rotation which has
already explained above. LR rotation = RR rotation + LL rotation, i.e.,
first RR rotation is performed on subtree and then LL rotation is
performed on full tree, by full tree we mean the first node from the
path of inserted node whose balance factor is other than -1, 0, or 1.
3. LR Rotation
3. LR Rotation
3. RL Rotation
• It is difficult to implement.
• It has high constant factors for some of the operations.
• Less used compared to Red-Black trees.
• Due to its rather strict balance, AVL trees provide complicated
insertion and removal operations as more rotations are performed.
• Take more processing for balancing.