0% found this document useful (0 votes)
16 views19 pages

AVL Tree

An AVL Tree is a height-balanced binary search tree that maintains a balance factor between -1 and 1 for each node, ensuring efficient operations with a time complexity of O(log n). Insertions and deletions may require rotations to maintain balance, with four types of rotations (LL, RR, LR, RL) used to correct imbalances. AVL Trees are advantageous for their self-balancing nature and faster lookups, but they can be complex to implement and manage compared to other tree structures like Red-Black Trees.

Uploaded by

OML series
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views19 pages

AVL Tree

An AVL Tree is a height-balanced binary search tree that maintains a balance factor between -1 and 1 for each node, ensuring efficient operations with a time complexity of O(log n). Insertions and deletions may require rotations to maintain balance, with four types of rotations (LL, RR, LR, RL) used to correct imbalances. AVL Trees are advantageous for their self-balancing nature and faster lookups, but they can be complex to implement and manage compared to other tree structures like Red-Black Trees.

Uploaded by

OML series
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

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

• 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. L L rotation: Inserted node is in the left subtree of left subtree of A
2. R R rotation : Inserted node is in the right subtree of right subtree of A
3. L R rotation : Inserted node is in the right subtree of left subtree of A
4. R L 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
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, which is applied on the edge
below a node having balance factor -2
• Node A has balance factor -2 because a node C is

• inserted in the right subtree of A right subtree.

• We perform the RR rotation on the edge below A


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, which is applied on the edge below a
node having balance factor
Node C has balance factor 2 because a node A is

inserted in the left subtree of C left subtree. We

perform the LL rotation on the edge below A.


3. LR Rotation

• 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

• R L 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.
4. RL Rotation
4. RL Rotation
Construct an AVL tree having the following elements
H, I, J, B, A, E, C, F, D, G, K, L
Applications of AVL Tree

• It is used to index huge records in a database and also to efficiently


search in that.
• For all types of in-memory collections, including sets and dictionaries,
AVL Trees are used.
• Database applications, where insertions and deletions are less
common but frequent data lookups are necessary
• Software that needs optimized search.
• It is applied in corporate areas and storyline games.
Advantages of AVL Tree

• AVL trees can self-balance themselves.


• It is surely not skewed.
• It provides faster lookups than Red-Black Trees
• Better searching time complexity compared to other trees like binary
tree.
• Height cannot exceed log(N), where, N is the total number of nodes in
the tree.
Disadvantages of AVL Tree

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

You might also like