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

DSA - Ch.4 [2]

Uploaded by

Akshat Joshi
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 views25 pages

DSA - Ch.4 [2]

Uploaded by

Akshat Joshi
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/ 25

UNIT 4

Tree
Prof. Pintu Chauhan, Assistant Professor
Information Technology & Engineering
Topic-2
AVL Tree
Balanced Tree
• Binary Search Tree gives advantage of Fast Search, but sometimes in few cases
we are not able to get this advantage. E.g. look into worst case BST
• Balanced binary trees are classified into two categories
Worst search time cases
– Height Balanced Tree (AVL Tree)
for Binary Search Tree
– Weight Balanced Tree
50 20

40 30

30 40

20 50
Height Balanced Tree (AVL Tree)
• A Balance factor of a node | HL - HR|<=1, where HL, HR are heights of left and
right sub-tree respectively
• Permissible balance factors:
– A Left-High (balance factor +1)
The left-sub tree is one level taller than the right-sub tree
– Balanced (balance factor 0)
The left and right sub-trees are both the same heights
– Right-High (balance factor -1)
The right sub-tree is one level taller than the left-sub tree.
• In height balanced tree, each node must be in one of these states
• If there exists a node in a tree where this is not true, then such a tree is called
Unbalanced
AVL Tree
0 -1

+1 -1
0 0

0 0 0 -1 +1 +1

0 0
-1
0
0

0
AVL Tree
Critical Node Critical Node
Unbalanced Node Unbalanced Node
0 -1
-1
0 +1

• Sometimes tree becomes unbalanced by inserting or deleting any node


• Then based on position of insertion, we need to rotate the unbalanced node
• Rotation is the process to make tree balanced
AVL Tree Rotation
The following operations are performed on AVL tree...
• Search
• Insertion
• Deletion
AVL Tree Rotation
There are four rotations and they are classified into two types.

AVL Tree Rotation

Single Rotation Double Rotation

Left Rotation Right Rotation Left Right Rotation Right Left Rotation
(LL Rotation) (RR Rotation) (LR Rotation) (RL Rotation)
Operation on an AVL
• In AVL tree, after performing operations like insertion and deletion we need to
check the balance factor of every node in the tree. Permissible balance factors:
• If every node satisfies the balance factor condition then we conclude the
operation otherwise we must make it balanced.
• If there exists a node in a tree where this is not true, then such a tree is called
Unbalanced
Rotation operations are used to make the tree balanced.

Rotation is the process of moving nodes either to left or to


right to make the tree balanced.
Single Left Rotation (LL Rotation )
• In LL Rotation, every node moves one position to left from the current position.
• To understand LL Rotation, let us consider the following insertion operation in
AVL Tree.
-2
Insert 8, 10, 12
-2 8 0
8 10
-1
10 0 0
10 -1
0 8 12
0
12
12
To make balanced we use LL After LL Rotation
Tree is imbalanced Rotation which moves nodes Tree is balanced
one position to left
Single Right Rotation (RR Rotation )
• In RR Rotation, every node moves one position to right from the current
position.
• To understand RR Rotation, let us consider the following insertion operation in
Insert 12, 10, 8
AVL Tree. +2 +2 0
12
12 10

+1 +1 10 0 0
10 8 12

0 8
8 0 To make balanced we use RR After RR Rotation
Tree is imbalanced Rotation which moves nodes Tree is balanced
one position to Right
Left Right Rotation (LR Rotation )
• 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.
• To understand LR Rotation, let us consider the following insertion operation in
AVL Tree. Insert 12, 8, 10
12 +2 +2 0
12 +2 12
10
-1 +1 0 0
-1 8
8 10
0 8 12
0
10 0
10 8 After LR Rotation
Tree is imbalanced LL Rotation RR Rotation Tree is balanced
Right Left Rotation (LR Rotation )
• 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.
• To understand RL Rotation, let us consider the following insertion operation in
AVL Tree. Insert 8, 12, 10 -2
8 -2 -2 12 0
12
10
+1 -1 0 0
+1 8
12 10
8 12
0 10 0 0
10 8 After RL Rotation
Tree is imbalanced RR Rotation LL Rotation Tree is balanced
AVL Tree Rotation
The following operations are performed on AVL tree...
• Search
• Insertion
• Deletion
Search Operation in AVL Tree
• In an AVL tree, the search operation is performed with O(log n) time complexity.
• The search operation in the AVL tree is similar to the search operation in a Binary
search tree.
• We use the following steps to search an element in AVL tree.
Step 1 - Read the search element from the user.

Step 2 - Compare the search element with the value of root node in the tree.

Step 3 - If both are matched, then display "Given node is found!!!" and terminate the
function
Step 4 - If both are not matched, then check whether search element is smaller or
larger than that node value.
Search Operation in AVL Tree
Step 5 - If search element is smaller, then continue the search process in left subtree.

Step 6 - If search element is larger, then continue the search process in right subtree.

Step 7 - Repeat the same until we find the exact element or until the search element is
compared with the leaf node.

Step 8 - If we reach to the node having the value equal to the search value, then
display "Element is found" and terminate the function.

Step 9 - If we reach to the leaf node and if it is also not matched with the search
element, then display "Element is not found" and terminate the function.
Insertion Operation in AVL Tree
• In an AVL tree, the insertion operation is performed with O(log n) time complexity.
• In AVL Tree, a new node is always inserted as a leaf node.
• The insertion operation is performed as follows.
Step 1 - Insert the new element into the tree using Binary Search Tree insertion logic.

Step 2 - After insertion, check the Balance Factor of every node.

Step 3 - If the Balance Factor of every node is 0 or 1 or -1 then go for next operation.

Step 4 - If the Balance Factor of any node is other than 0 or 1 or -1 then that tree is
said to be imbalanced. In this case, perform suitable Rotation to make it balanced
and go for next operation.
Construct an AVL tree by inserting numbers from 1 to 7
Insert 1
0
1 Tree is balanced

Insert 2 -1
1
0 Tree is balanced
2
Construct an AVL tree by inserting numbers from 1 to 7
Insert 3
-2
1 1
0
-1 2
After LL
2 2 Rotation 0 0
0
3 1 3
3
Tree is Imbalanced LL Rotation Tree is Balanced
Construct an AVL tree by inserting numbers from 1 to 7
Insert 4

-1
2

0 -1 Tree is balanced
1 3

0
4
Construct an AVL tree by inserting numbers from 1 to 7
Insert 5
-1 -2 -2
2 2

0 -1 -2 0 -2 0
0
1 3 After LL 2
1 3 Rotation at 3
0
0
0 -1 -1
1 4
4 4
Node is Imbalanced
0 0
0 0 3 5
5 5
Tree is balanced
LL Rotation at 3
Construct an AVL tree by inserting numbers from 1 to 7
Insert 6 Node is Imbalanced
0 -2 0
-2
0 22 4
2
0 0 -1 0
0 After LL 0
-1 Rotation at 2
1 4 2 5
1 4
0 0 0 -1 0 0
0 -1 0
3 5 3 6
3 5 1
0
0
6 Tree is balanced
6
LL Rotation at 2
Construct an AVL tree by inserting numbers from 1 to 7
Insert 7
0 -1 0
Node is Imbalanced 4 After LL
4 Rotation at 5
0
0 0 0 0
0 -2 4
2 5 2 5 0 0
0
0 0 0 0 -1 0 0 0
2 6
3 6 1 3 6 0 0 0
1
1 3 5
0 0 7
LL Rotation at 5
7 7 Tree is Balanced
Deletion Operation in AVL Tree
• The deletion operation in AVL Tree 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 go for next operation otherwise perform
suitable rotation to make the tree Balanced.
www.paruluniversity.ac.in

You might also like