0% found this document useful (0 votes)
9 views33 pages

Avl Trees

An AVL tree is a self-balancing binary search tree where the height difference between the left and right subtrees of any node is at most one. It maintains a balance factor for each node to ensure balance after operations like insertion and deletion, utilizing rotation techniques such as LL, RR, LR, and RL rotations. The document also discusses the deletion operation and specific rotations required to maintain balance after deletions.

Uploaded by

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

Avl Trees

An AVL tree is a self-balancing binary search tree where the height difference between the left and right subtrees of any node is at most one. It maintains a balance factor for each node to ensure balance after operations like insertion and deletion, utilizing rotation techniques such as LL, RR, LR, and RL rotations. The document also discusses the deletion operation and specific rotations required to maintain balance after deletions.

Uploaded by

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

AVL TREES

BY:-
CHARANPREET KAUR
AVL Tree
 AVL tree is a self balanced binary search tree.
 That means, an AVL tree is also a binary search tree but it is
a balanced tree.
 A binary tree is said to be balanced, if the difference between
the heights of left and right subtrees of every node in the tree
is either -1, 0 or +1.
 In other words, a binary tree is said to be balanced if for
every node, height of its children differ by at most one.
 In an AVL tree, every node maintains a extra information
known as balance factor.
 The AVL tree was introduced in the year of 1962 by G.M.
Adelson-Velsky and E.M. Landis.
Right and Left Skewed Binary
Search Tree
 Balance factor of a node is the
difference between the heights of left
and right subtrees of that node.
 The balance factor of a node is

calculated either height of left


subtree - height of right
subtree (OR) height of right
subtree - height of left subtree.
Example:

The above tree is a binary search tree and every node is


satisfying balance factor condition. So this tree is said to be
an AVL tree.
AVL Tree Rotations
 In AVL tree, after performing every operation like
insertion and deletion we need to check the balance
factor of every node in the tree.
 If every node satisfies the balance factor condition then

we conclude the operation otherwise we must make it


balanced.
 We use rotation operations to make the tree balanced

whenever the tree is becoming imbalanced due to any


operation.

Rotation operations are used to make a tree balanced.


There are four rotations and they are classified
into two types.
ROTATIONS IN INSERTION
Single Left Rotation (LL
Rotation)
 In LL Rotation, the node is inserted to the
left subtree of left subtree of node A
 When LL Rotation is applied, the node is

rotated towards right to make the tree


balanced.
Single Right Rotation (RR Rotation)
 In RR Rotation, the node is inserted to the
right subtree of right subtree of node A
 When RR Rotation is applied, the node is

rotated left to make the tree balanced.


EXAMPLE:-
Left Right Rotation (LR Rotation)
 The LR Rotation is combination of single
left rotation followed by single right
rotation.
 In LR Rotation, first every node moves

one position to left then one position to


right from the current position.
Right Left Rotation (RL Rotation)

 The RL Rotation is combination of single


right rotation followed by single left
rotation.
 In RL Rotation, first every node moves one

position to right then one position to left


from the current position.
Example: Construct an AVL Tree by inserting
numbers from 1 to 8.
RR ROTATION
RR ROTATION

RR ROTATION
RR ROTATION
Exercise—Do It yourself
 Build an AVL tree with the following values:
15, 20, 24, 10, 13, 7, 30, 36, 25
 Solution:
Deletion Operation in AVL Tree

 In an AVL Tree, the deletion operation is


similar to deletion operation in BST.
 But after every deletion operation we need

to check with the Balance Factor condition.


 If the tree is balanced after deletion then go

for next operation otherwise perform the


suitable rotation to make the tree Balanced.
ROTATIONS INCASE OF
DELETION
1. R0 ROTATION
2. R1 ROTATION
3. R-1 ROTATION
4. L0 ROTATION
5. L1 ROTATION
6. L-1 ROTATION
R0, R1 and R-1Rotation
 R0, R1 and R-1Rotation can be applied if
there is deletion in the right subtree of the
AVL Tree i.e. R stands for deletion in the
right subtree of an AVL Tree.
 The subscript 0,1,-1 will be according to

the Balance Factor of the root of the


opposite subtree i.e. incase of R0, R1, R-1
Rotation, 0,1,-1 will be the Balance Factor
of the root of the Left Subtree.
R0 Rotation
EXAMPLE
R1
Rotation
R-1 Rotation
EXAMPLE
SOLVE:-
L0, L1 and L-1Rotation
 Similarly L0, L1 and L-1Rotation can be
applied if there is deletion in the left subtree
of the AVL Tree
 The subscript 0,1,-1 will be according to the

Balance Factor of the root of the opposite


subtree.

You might also like