0% found this document useful (0 votes)
43 views

What Is Avl Tree

An AVL tree is a self-balancing binary search tree where the difference between the heights of left and right subtrees of any node is at most one. AVL trees provide faster search, insertion, and deletion operations compared to regular binary search trees through self-balancing after each operation. AVL trees use rotations to maintain the balance factor property during insertions and deletions.

Uploaded by

Kashif Riaz
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)
43 views

What Is Avl Tree

An AVL tree is a self-balancing binary search tree where the difference between the heights of left and right subtrees of any node is at most one. AVL trees provide faster search, insertion, and deletion operations compared to regular binary search trees through self-balancing after each operation. AVL trees use rotations to maintain the balance factor property during insertions and deletions.

Uploaded by

Kashif Riaz
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/ 5

What is AVL TREE?

(Adelson-Velsky and Landis)


An AVL is a self-balancing Binary Search Tree (BST) where the difference between
the heights of left and right sub trees of any node cannot be more than one.
KEY POINTS
It is height balanced tree
It is a binary search tree
It is a binary tree in which the height difference between the left subtree and
right subtree is almost one
Height is the maximum depth from root to leaf
Characteristics of AVL Tree:
It follows the general properties of a Binary Search Tree.
Each subtree of the tree is balanced, i.e., the difference between the height of the
left and right subtrees is at most 1.
The tree balances itself when a new node is inserted. Therefore, the insertion
operation is time-consuming
Application of AVL Tree:
Most in-memory sets and dictionaries are stored using AVL trees.
Database applications, where insertions and deletions are less common but
frequent data lookups are necessary, also frequently employ AVL trees.
In addition to database applications, it is employed in other applications that call
for better searching.
Most STL implementations of the ordered associative containers (sets, multisets,
maps and multimaps) use red-black trees instead of AVL trees.
Advantages of AVL Tree:
AVL trees can self-balance.
It also provides faster search operations.
AVL trees also have balancing capabilities with a different type of rotation
Better searching time complexity than other trees, such as the binary Tree.
Height must not be greater than log(N), where N is the total number of nodes in
the Tree.
Disadvantages of AVL Tree:
AVL trees are difficult to implement
AVL trees have high constant factors for some operations.

Following are the Difference between Binary Search Tree and AVL Tree

S.No Binary Search Tree AVL Tree

In Binary Search Tree, In AVL


In AVL Tree, every node follows the
1. Tree, every node does not follow
balance factor i.e. 0, 1, -1.
the balance factor.

Every Binary Search tree is not an Every AVL tree is a Binary Search
2.
AVL tree. tree.

3. Simple to implement. Complex to implement.

The height or depth of the tree is The height or depth of the tree is
4.
O(n). O(logn)

Searching is efficient because


Searching is not efficient when
searching for the desired node is
5. there are a large number of
faster due to the balancing of
nodes in the Binary Search tree.
height.
S.No Binary Search Tree AVL Tree

The Binary Search tree structure AVL tree structure consists of 4


6. consists of 3 fields left subtree, fields left subtree, data, right
data, and right subtree. subtree, and balancing factor.

7. It is not a balanced tree. It is a balanced tree.

In an AVL tree, Insertion and


In Binary Search tree. Insertion
deletion are complex as it requires
8. and deletion are easy because no
multiple rotations to balance the
rotations are required.
tree.

AVL trees have an additional guarantee:


1. The difference between the depth of right and left sub-trees cannot be
more than one. This difference is called the balance factor.

In order to maintain this guarantee, an implementation of an AVL will


include an algorithm to rebalance the tree when adding an additional
element would upset this guarantee
AVL trees have a worst case lookup, insert, and delete time of O(log n), where n is
the number of nodes in the tree. The worst case space complexity is O(n).
AVL Insertion Process
Insertion in an AVL tree is similar to insertion in a binary search tree. But after
inserting and element, you need to fix the AVL properties using left or right
rotations:
 If there is an imbalance in the left child's right sub-tree, perform a left-right
rotation
 If there is an imbalance in the left child's left sub-tree, perform a right
rotation
 If there is an imbalance in the right child's right sub-tree, perform a left
rotation
 If there is an imbalance in the right child's left sub-tree, perform a right-left
rotation
AVL Tree Rotations
In AVL trees, after each operation like insertion and deletion, the balance factor of
every node needs to be checked. If every node satisfies the balance factor
condition, then the operation can be concluded. Otherwise, the tree needs to be
rebalanced using rotation operations.
There are four rotations and they are classified into two types:
Left Rotation (LL Rotation)
In left rotations, every node moves one position to left from the current position.

Right Rotation (RR Rotation)


In right rotations, every node moves one position to right from the current
position.
Left-Right Rotation (LR Rotation)
Left-right rotations are a combination of a single left rotation followed by a single
right rotation.

First, every node moves one position to the left, then one position to the right
from the current position.
Right-Left Rotation (RL Rotation)
Right-left rotations are a combination of a single right rotation followed by a
single left rotation.
First, every node moves one position to the right then, then one position to the
left from the current position.

You might also like