Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

AVL Tree

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

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.

An AVL tree is given in the following figure. We can see that, balance factor associated with
each node is in between -1 and +1. therefore, it is an example of AVL tree.
Complexity

Algorithm Average case Worst case

Space o(n) o(n)

Search o(log n) o(log n)

Insert o(log n) o(log n)

Delete o(log n) o(log n)

Operations on AVL tree

Due to the fact that, AVL tree is also a binary search tree therefore, all the operations are
performed in the same way as they are performed in a binary search tree. Searching and
traversing do not lead to the violation in property of AVL tree. However, insertion and
deletion are the operations which can violate this property and therefore, they need to be
revisited.

S.No. Operation Description

1 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.

2 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.

3 Searching It is similar to performing a search in BST


Highlights:

1. Balance Factor represents the height difference between the left and the right sub-
trees for a given node.
2. For leaf nodes, the balance factor is 0.
3. AVL balance criteria: bf = -1, 0, +1 for all nodes.

AVL Rotations

We perform rotation in AVL tree only in case if Balance Factor is other than -1, 0, and 1.
There are basically four types of rotations which are as follows:

1. LL rotation: Inserted node is in the left subtree of left subtree of A


2. RR rotation: Inserted node is in the right subtree of right subtree of A
3. LR rotation: Inserted node is in the right subtree of left subtree of A
4. RL rotation: Inserted node is in the left subtree of right subtree of A

Where node A is the node whose balance Factor is other than -1, 0, 1.

The first two rotations LL and RR are single rotations and the next two rotations LR and RL
are double rotations. For a tree to be unbalanced, minimum height must be at least 2, Let us
understand each rotation

1. RR Rotation

When BST becomes unbalanced, due to a node is inserted into the right subtree of the right
subtree of A, then we perform RR rotation, RR rotation is an anticlockwise rotation.

2. LL Rotation

When BST becomes unbalanced, due to a node is inserted into the left subtree of the left
subtree of C, then we perform LL rotation, LL rotation is clockwise rotation.
3. LR Rotation

Double rotations are bit tougher than single rotation. In some cases, a single tree rotation is
not sufficient to balance the tree after a height-affecting operation. One such case is the
Left-Right (LR) insertion, where an imbalance occurs when a node is inserted into the right
subtree of the left child of the imbalanced node.

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.

4. RL Rotation

It is similar to LR rotation but it is performed when the tree gets unbalanced, upon insertion
of a node into the left subtree of the right child of the imbalance node.

RL rotation = LL rotation + RR rotation, i.e., first LL rotation is performed on subtree and


then RR 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.
NOTE:

1. Rotations are done only on three nodes (including the imbalanced node)
irrespective of the size of the Binary Search Tree. Hence, in the case of a large tree
always focus on the two nodes around the imbalanced node and perform the tree
rotations.
2. Upon insertion of a new node, if multiple nodes get imbalanced then traverse the
ancestors of the inserted node in the tree and perform rotations on the first
occurred imbalanced node. Continue this process until the whole tree is balanced.
This process is knowns as retracing which is discussed later in the article.

Deletion in AVL Tree

Deleting a node from an AVL tree is similar to that in a binary search tree. Deletion may
disturb the balance factor of an AVL tree and therefore the tree needs to be rebalanced in
order to maintain the AVLness. For this purpose, we need to perform rotations. The two
types of rotations are L rotation and R rotation.

If the node which is to be deleted is present in the left sub-tree of the critical node, then L
rotation needs to be applied else if, the node which is to be deleted is present in the right
sub-tree of the critical node, the R rotation will be applied.

Let us consider that, A is the critical node and B is the root node of its left sub-tree. If node
X, present in the right sub-tree of A, is to be deleted, then there can be three different
situations:

 R0 rotation (Node B has balance factor 0)

 R1 Rotation (Node B has balance factor 1)

 R-1 Rotation (Node B has balance factor -1)


Let us consider that, A is the critical node and B is the root node of its right sub-tree. If node
X, present in the left sub-tree of A, is to be deleted, then there can be three different
situations:

 L0 rotation (Node B has balance factor 0)

 L1 Rotation (Node B has balance factor 1)

 L-1 Rotation (Node B has balance factor -1)

AVL Tree is a self-balancing binary search tree. The deletion of a node will be the same
as in BST. After a node has been deleted according to BST, we’ll check for the
unbalanced nodes (Nodes having a balance factor of more than 1).

Steps to follow for deletion


Here are the steps to follow for deleting a node in an AVL tree:
1. First, remove the node you want to delete, like when removing a standard item
from a list.

2. After removing the node, go up the tree and update the heights of the nodes. It
is like updating the order of the steps when you climb up or down stairs.

3. Starting from where you deleted the node, go up towards the root. Along the
way, check if any node needs to be balanced.

4. If you find an imbalance, perform rotations (simple or double) to balance the


tree.

Applications of AVL Tree:


1. It is used to index huge records in a database and also to efficiently search in
that.
2. For all types of in-memory collections, including sets and dictionaries, AVL Trees
are used.
3. Database applications, where insertions and deletions are less common but
frequent data lookups are necessary
4. Software that needs optimized search.
5. It is applied in corporate areas and storyline games.
Advantages of AVL Tree:
1. AVL trees can self-balance themselves.
2. It is surely not skewed.
3. It provides faster lookups than Red-Black Trees
4. Better searching time complexity compared to other trees like binary tree.
5. Height cannot exceed log(N), where, N is the total number of nodes in the tree.
Disadvantages of AVL Tree:
1. It is difficult to implement.
2. It has high constant factors for some of the operations.
3. Less used compared to Red-Black trees.
4. Due to its rather strict balance, AVL trees provide complicated insertion and
removal operations as more rotations are performed.
5. Take more processing for balancing.

Highlights:

1. Rotations are performed to maintain the AVL Balance criteria.


2. Rotation is a process of changing the structure without affecting the elements' order.
3. Rotations are done on an unbalanced node based on the location of the newly
inserted node.
4. Single rotations include LL (clockwise) and RR (anti-clockwise) rotations.
5. Double rotations include LR (RR + LL) and RL (LL + RR) rotations.
6. Rotations are done only on 3 nodes, including the unbalanced node.

You might also like