0% found this document useful (0 votes)
19 views23 pages

Avl Tree

Uploaded by

Pratham Nilange
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)
19 views23 pages

Avl Tree

Uploaded by

Pratham Nilange
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

AVL Tree

named after inventors


Adelson-Velsky and Landis
Balance factor

In a binary tree the balance factor of a node is defined to be the height


difference of its two child sub-trees.

BalanceFactor(Node)=Height(LeftSubtree(node))-Height(RightSubtree(node))
AVL Tree : a tree based data structure

 a self-balancing search tree.

 balanced binary search tree.

 height-balanced binary search tree.

 balance factor of every node is either -1, 0 or +1


NOTE:

 A binary tree is said to be balanced if the height of left and right children of every node
differ by either -1, 0 or +1.
 In an AVL tree, every node maintains an extra information known as balance factor.
 Every AVL Tree is a binary search tree but every Binary Search Tree need not be AVL
tree.

 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.
Advantage of AVL tree

• AVL tree controls the height of the binary search tree by not letting it
to be skewed.
• 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.
AVL tree rotations
• Rotation is the process of moving nodes either to left or to right to make the tree
balanced.
• Rotation in AVL tree is performed only in case if Balance Factor is other than -1, 0,
and 1.
• There are 4 types of rotations
• Single Rotation
• LL imbalance  Single right Rotation
• RR imbalance  Single left Rotation
• Double Rotation
• LR imbalance  Double RL Rotation
• RL imbalance  Double LR Rotation
Single Left Rotation (Due to RR imbalance)
In Single left Rotation, every node moves one position to left from the current
position. Imbalance was caused by the insertion of a new node as a right child in
the right subtree of A, where node A is the node whose balance Factor is other
than -1, 0, 1.
Single Right Rotation (Due to LL imbalance)
In Single Right Rotation, every node moves one position to right from the current
position. Imbalance was caused by the insertion of a new node as a left child in the
left subtree of A, where node A is the node whose balance Factor is other than -1,
0, 1.
Double LR Rotation (Due to RL imbalance)
The LR Rotation is a sequence of single left rotation followed by a single right rotation. In LR
Rotation, at first, every node moves one position to the left and one position to right from the
current position. Imbalance was caused by the insertion of a new node as a right child in the left
subtree of A, where node A is the node whose balance Factor is other than -1, 0, 1.
Double RL Rotation (Due to LR imbalance)
The RL Rotation is a sequence of single right rotation followed by a single left rotation. In RL
Rotation, at first, every node moves one position to the right and one position to left from the
current position. Imbalance was caused by the insertion of a new node as a left child in the right
subtree of A, where node A is the node whose balance Factor is other than -1, 0, 1.
AVL tree 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 RL rotation


• If there is an imbalance in the left child's left sub-tree, perform a Single Right rotation
• If there is an imbalance in the right child's right sub-tree, perform a Single Left rotation
• If there is an imbalance in the right child's left sub-tree, perform a LR rotation
General form of Single Right Rotation in AVL Tree
General form of Single Left Rotation in AVL Tree
General form of LR Rotation due to insertion of Right child in
the Left subtree (RL imbalance) in AVL Tree
General form of RL Rotation due to insertion of Left child in
the Right subtree (LR imbalance) in AVL Tree
Construct AVL tree: 50, 80, 30, 70, 75

Construct AVL tree: 5, 6, 8, 3, 2, 4, 7

Construct BST tree: 10, 20, 3, 7, 8, 4, 2 : Check for AVL tree.


Add node (12) : Check for AVL tree
Insertion of elements X , Y in sequence (i.e first insert X then insert Y) results in
right rotation in the following AVL Tree. Identify X,Y.
X=20, Y=10
X=20, Y=35
X=35, Y=10
50 X=10, Y=20

30 80

70
Insertion of ______________ element(s) results in left right (LR) rotation in the
following AVL Tree.

2 10

17
________ rotation is required to balance the following AVL tree .

70

50 80

90
40 60

85
Insertion of elements (s) _________________ in sequence does not require any
rotation in the following AVL tree.

50

20 70

100
10,25,60
10,5,60
5,10,60
10,7
None of these
Insertion of elements (s) _________________ results in Left Right (LR) rotation in
the following AVL tree.

50

20 70

100
15, 10
299
10, 15
90
None of these
AVL tree visualization

[Link]

You might also like