SlideShare a Scribd company logo
1 of 46
WHAT IS BINARY SEARCH TREE
• Every node contain only two
child.
• Small value at left child node
and large value at right child
node as compare to Root node.
• The main advantage of BST is
that it is easy to perform
operations like inserting,
searching, traversing and
deleting.
Prepared By: Awais Ahmad
 The disadvantage of skewed binary search tree is that
the worst case time complexity of a search is O(n).
50
40 60
30 45 55 70
40
50
30
20
10
Symmetric Skewed
Prepared By: Awais Ahmad
 There is a need to maintain the binary search tree to
be of the balanced height, so that it is possible to
obtained for the search option a time complexity of
O(log N) in the worst case.
 One of the most popular balanced tree was introduced
by
Prepared By: Awais Ahmad
A binary tree is said to be an AVL tree if T is a
root of tree and T(L) is its left sub tree and
T(R) is its right sub-tree of tree T and H(T(L))
and H(T(R)) are the heights of the left and
right sub-trees of T respectively, and |H(T(L))
- H(T(R))|<= 1 Then we called T is AVL tree.
Height of left sub-tree minus height of Right
left sub-tree
[H(T(L)) - H(T(R))]
Note:
 An empty binary tree is an AVL Tree
 Balance Factor should be
-1 0 1
T
T(L) T(R)
Prepared By: Awais Ahmad
T
T(L) T(R)
Prepared By: Awais Ahmad
40
50
30
Prepared By: Awais Ahmad
 If after the insertion of the element the, the
balance factor of any node is affect this
problem is over come by using rotation.
 Rotation is use to restore the balance of
search tree.
Prepared By: Awais Ahmad
 To perform the rotation
it is necessary to identify
a specific node A whose
balance factor (BF) is
neither 0, -1 0r 1 and
which is the nearest
ancestor to the inserted
node on the path from
the inserted node to the
root.
40
50
30
Prepared By: Awais Ahmad
LL(Left Left) rotation.
RR(Right Right) rotation.
RL(Right Left) rotation.
LR( Left Right) rotation
Prepared By: Awais Ahmad
 Single rotation switches the roles of the parent and
child while maintaining the search order.
 We rotate a node and its child, child becomes parent
 Parent becomes Right child in LL Rotation.
 Parent becomes Left child in RR Rotation.
Prepared By: Awais Ahmad
 Inserted node is in the
left sub-tree of the left
sub-tree of A.
 For LL Rotations identify
A and B, where B is a
Left child of A because
insertion is on left side.
 Then make A as a child
of B.
40
50
30
Prepared By: Awais Ahmad
40
50
30
50
40
30
Parent becomes
Right child
Prepared By: Awais Ahmad
 Inserted node is in the
right sub-tree of the
Right sub-tree of A.
 For RR Rotations
identify A and B,
where B is a Right
child of A because
insertion is on Right
side.
 Then make A as a child
of B.
60
50
70
Prepared By: Awais Ahmad
60
50
70
70
60
50
Parent becomes
Left child
Prepared By: Awais Ahmad
 Single rotation does not fix the LR rotation and RL
rotation.
 LR and RL rotations require a double rotation,
involving three node.
 Double rotation is equivalent to a sequence of two
single rotation.
 1st rotation on original tree
 2nd rotation on the new tree
Prepared By: Awais Ahmad
 Inserted node is in the
right sub-tree of the left
sub-tree of A.
 For LR Rotations identify A
B and C, where B is a Left
child of A and C is right
child of B because Inserted
node is in the right sub-
tree of the left sub-tree of
A.
 Then make A and B as a
child of C.
40
50
45
Prepared By: Awais Ahmad
40
50
45
50
45
40
Prepared By: Awais Ahmad
 Inserted node is in the left
sub-tree of the Right sub-
tree of A .
 For RL Rotations identify A
B and C, where B is a Right
child of A and C is Left
child of B because Inserted
node is in the left sub-tree
of the Right sub-tree of A.
 Then make A and B as a
child of C.
60
50
55
Prepared By: Awais Ahmad
60
50
55
60
55
50
Prepared By: Awais Ahmad
 // If this node becomes unbalanced, then there are 4 cases

 // Left Left Case
 if (balance > 1 && key < node->left->key)
 return rightRotate(node);

 // Right Right Case
 if (balance < -1 && key > node->right->key)
 return leftRotate(node);

 // Left Right Case
 if (balance > 1 && key > node->left->key)
 {
 node->left = leftRotate(node->left);
 return rightRotate(node);
 }

 // Right Left Case
 if (balance < -1 && key < node->right->key)
 {
 node->right = rightRotate(node->right);
 return leftRotate(node);
 }
Prepared By: Awais Ahmad
Prepared By: Awais Ahmad
5
Prepared By: Awais Ahmad
5
7
Prepared By: Awais Ahmad
5
7
19
Prepared By: Awais Ahmad
5
7
19
19
7
5
Prepared By: Awais Ahmad
19
7
5
Prepared By: Awais Ahmad
19
7
5
12
Prepared By: Awais Ahmad
19
7
5
12
10
Prepared By: Awais Ahmad
19
7
5
12
10
7
5
19
12
10
Prepared By: Awais Ahmad
7
5
19
12
10
Prepared By: Awais Ahmad
7
5
19
12
10
15
Prepared By: Awais Ahmad
7
5
19
12
10
15
12
7 19
155 10
Prepared By: Awais Ahmad
12
7 19
155 10
Prepared By: Awais Ahmad
12
7 19
155 10
18
Prepared By: Awais Ahmad
12
7 19
155 10
18
12
7
5 10
18
15 19
Prepared By: Awais Ahmad
12
7
5 10
18
15 19
Prepared By: Awais Ahmad
12
7
5 10
18
15 19
20
Prepared By: Awais Ahmad
12
7
5 10
18
15 19
20
25
Prepared By: Awais Ahmad
12
7
5 10
18
15 19
20
25
Prepared By: Awais Ahmad
12
7
5 10
18
15 20
19 25
Prepared By: Awais Ahmad
12
7
5 10
18
15 20
19 25
23
Prepared By: Awais Ahmad
12
7
5 10
18
15 20
19 25
23
Prepared By: Awais Ahmad
12
7
5 10
20
18 25
2315 19
Prepared By: Awais Ahmad
Prepared By: Awais Ahmad
Prepared By: Awais Ahmad
BS-SE-A
UOG Lahore Campus

More Related Content

Similar to AVL Tree

Avl Tree Implementation
Avl Tree ImplementationAvl Tree Implementation
Avl Tree ImplementationEhsan Elahi
 
AVL Tree in Data Structure
AVL Tree in Data Structure AVL Tree in Data Structure
AVL Tree in Data Structure Meghaj Mallick
 
Data Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL TreesData Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL TreesManishPrajapati78
 
Data structures trees and graphs - AVL tree.pptx
Data structures trees and graphs - AVL  tree.pptxData structures trees and graphs - AVL  tree.pptx
Data structures trees and graphs - AVL tree.pptxMalligaarjunanN
 
Lect 13, 14 (final)AVL Tree and Rotations.pdf
Lect 13, 14 (final)AVL Tree and Rotations.pdfLect 13, 14 (final)AVL Tree and Rotations.pdf
Lect 13, 14 (final)AVL Tree and Rotations.pdfMuhammadUmerIhtisham
 
Adelson velskii Landis rotations based on
Adelson velskii Landis rotations based onAdelson velskii Landis rotations based on
Adelson velskii Landis rotations based onbanupriyar5
 

Similar to AVL Tree (10)

Avl Tree Implementation
Avl Tree ImplementationAvl Tree Implementation
Avl Tree Implementation
 
AVL Tree in Data Structure
AVL Tree in Data Structure AVL Tree in Data Structure
AVL Tree in Data Structure
 
Avl tree
Avl treeAvl tree
Avl tree
 
Data Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL TreesData Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL Trees
 
DS_Mod4_2.pdf
DS_Mod4_2.pdfDS_Mod4_2.pdf
DS_Mod4_2.pdf
 
4. avl
4. avl4. avl
4. avl
 
AVL tree PPT.pptx
AVL tree PPT.pptxAVL tree PPT.pptx
AVL tree PPT.pptx
 
Data structures trees and graphs - AVL tree.pptx
Data structures trees and graphs - AVL  tree.pptxData structures trees and graphs - AVL  tree.pptx
Data structures trees and graphs - AVL tree.pptx
 
Lect 13, 14 (final)AVL Tree and Rotations.pdf
Lect 13, 14 (final)AVL Tree and Rotations.pdfLect 13, 14 (final)AVL Tree and Rotations.pdf
Lect 13, 14 (final)AVL Tree and Rotations.pdf
 
Adelson velskii Landis rotations based on
Adelson velskii Landis rotations based onAdelson velskii Landis rotations based on
Adelson velskii Landis rotations based on
 

Recently uploaded

Filters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility ApplicationsFilters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility ApplicationsMathias Magdowski
 
Software Engineering Practical File Front Pages.pdf
Software Engineering Practical File Front Pages.pdfSoftware Engineering Practical File Front Pages.pdf
Software Engineering Practical File Front Pages.pdfssuser5c9d4b1
 
handbook on reinforce concrete and detailing
handbook on reinforce concrete and detailinghandbook on reinforce concrete and detailing
handbook on reinforce concrete and detailingAshishSingh1301
 
Insurance management system project report.pdf
Insurance management system project report.pdfInsurance management system project report.pdf
Insurance management system project report.pdfKamal Acharya
 
Maximizing Incident Investigation Efficacy in Oil & Gas: Techniques and Tools
Maximizing Incident Investigation Efficacy in Oil & Gas: Techniques and ToolsMaximizing Incident Investigation Efficacy in Oil & Gas: Techniques and Tools
Maximizing Incident Investigation Efficacy in Oil & Gas: Techniques and Toolssoginsider
 
Independent Solar-Powered Electric Vehicle Charging Station
Independent Solar-Powered Electric Vehicle Charging StationIndependent Solar-Powered Electric Vehicle Charging Station
Independent Solar-Powered Electric Vehicle Charging Stationsiddharthteach18
 
5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...archanaece3
 
Autodesk Construction Cloud (Autodesk Build).pptx
Autodesk Construction Cloud (Autodesk Build).pptxAutodesk Construction Cloud (Autodesk Build).pptx
Autodesk Construction Cloud (Autodesk Build).pptxMustafa Ahmed
 
engineering chemistry power point presentation
engineering chemistry  power point presentationengineering chemistry  power point presentation
engineering chemistry power point presentationsj9399037128
 
Adsorption (mass transfer operations 2) ppt
Adsorption (mass transfer operations 2) pptAdsorption (mass transfer operations 2) ppt
Adsorption (mass transfer operations 2) pptjigup7320
 
Tembisa Central Terminating Pills +27838792658 PHOMOLONG Top Abortion Pills F...
Tembisa Central Terminating Pills +27838792658 PHOMOLONG Top Abortion Pills F...Tembisa Central Terminating Pills +27838792658 PHOMOLONG Top Abortion Pills F...
Tembisa Central Terminating Pills +27838792658 PHOMOLONG Top Abortion Pills F...drjose256
 
analog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptxanalog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptxKarpagam Institute of Teechnology
 
Seizure stage detection of epileptic seizure using convolutional neural networks
Seizure stage detection of epileptic seizure using convolutional neural networksSeizure stage detection of epileptic seizure using convolutional neural networks
Seizure stage detection of epileptic seizure using convolutional neural networksIJECEIAES
 
Artificial Intelligence in due diligence
Artificial Intelligence in due diligenceArtificial Intelligence in due diligence
Artificial Intelligence in due diligencemahaffeycheryld
 
Interfacing Analog to Digital Data Converters ee3404.pdf
Interfacing Analog to Digital Data Converters ee3404.pdfInterfacing Analog to Digital Data Converters ee3404.pdf
Interfacing Analog to Digital Data Converters ee3404.pdfragupathi90
 
UNIT-2 image enhancement.pdf Image Processing Unit 2 AKTU
UNIT-2 image enhancement.pdf Image Processing Unit 2 AKTUUNIT-2 image enhancement.pdf Image Processing Unit 2 AKTU
UNIT-2 image enhancement.pdf Image Processing Unit 2 AKTUankushspencer015
 
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024EMMANUELLEFRANCEHELI
 
History of Indian Railways - the story of Growth & Modernization
History of Indian Railways - the story of Growth & ModernizationHistory of Indian Railways - the story of Growth & Modernization
History of Indian Railways - the story of Growth & ModernizationEmaan Sharma
 
Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..MaherOthman7
 
Fuzzy logic method-based stress detector with blood pressure and body tempera...
Fuzzy logic method-based stress detector with blood pressure and body tempera...Fuzzy logic method-based stress detector with blood pressure and body tempera...
Fuzzy logic method-based stress detector with blood pressure and body tempera...IJECEIAES
 

Recently uploaded (20)

Filters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility ApplicationsFilters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility Applications
 
Software Engineering Practical File Front Pages.pdf
Software Engineering Practical File Front Pages.pdfSoftware Engineering Practical File Front Pages.pdf
Software Engineering Practical File Front Pages.pdf
 
handbook on reinforce concrete and detailing
handbook on reinforce concrete and detailinghandbook on reinforce concrete and detailing
handbook on reinforce concrete and detailing
 
Insurance management system project report.pdf
Insurance management system project report.pdfInsurance management system project report.pdf
Insurance management system project report.pdf
 
Maximizing Incident Investigation Efficacy in Oil & Gas: Techniques and Tools
Maximizing Incident Investigation Efficacy in Oil & Gas: Techniques and ToolsMaximizing Incident Investigation Efficacy in Oil & Gas: Techniques and Tools
Maximizing Incident Investigation Efficacy in Oil & Gas: Techniques and Tools
 
Independent Solar-Powered Electric Vehicle Charging Station
Independent Solar-Powered Electric Vehicle Charging StationIndependent Solar-Powered Electric Vehicle Charging Station
Independent Solar-Powered Electric Vehicle Charging Station
 
5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...
 
Autodesk Construction Cloud (Autodesk Build).pptx
Autodesk Construction Cloud (Autodesk Build).pptxAutodesk Construction Cloud (Autodesk Build).pptx
Autodesk Construction Cloud (Autodesk Build).pptx
 
engineering chemistry power point presentation
engineering chemistry  power point presentationengineering chemistry  power point presentation
engineering chemistry power point presentation
 
Adsorption (mass transfer operations 2) ppt
Adsorption (mass transfer operations 2) pptAdsorption (mass transfer operations 2) ppt
Adsorption (mass transfer operations 2) ppt
 
Tembisa Central Terminating Pills +27838792658 PHOMOLONG Top Abortion Pills F...
Tembisa Central Terminating Pills +27838792658 PHOMOLONG Top Abortion Pills F...Tembisa Central Terminating Pills +27838792658 PHOMOLONG Top Abortion Pills F...
Tembisa Central Terminating Pills +27838792658 PHOMOLONG Top Abortion Pills F...
 
analog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptxanalog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptx
 
Seizure stage detection of epileptic seizure using convolutional neural networks
Seizure stage detection of epileptic seizure using convolutional neural networksSeizure stage detection of epileptic seizure using convolutional neural networks
Seizure stage detection of epileptic seizure using convolutional neural networks
 
Artificial Intelligence in due diligence
Artificial Intelligence in due diligenceArtificial Intelligence in due diligence
Artificial Intelligence in due diligence
 
Interfacing Analog to Digital Data Converters ee3404.pdf
Interfacing Analog to Digital Data Converters ee3404.pdfInterfacing Analog to Digital Data Converters ee3404.pdf
Interfacing Analog to Digital Data Converters ee3404.pdf
 
UNIT-2 image enhancement.pdf Image Processing Unit 2 AKTU
UNIT-2 image enhancement.pdf Image Processing Unit 2 AKTUUNIT-2 image enhancement.pdf Image Processing Unit 2 AKTU
UNIT-2 image enhancement.pdf Image Processing Unit 2 AKTU
 
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
 
History of Indian Railways - the story of Growth & Modernization
History of Indian Railways - the story of Growth & ModernizationHistory of Indian Railways - the story of Growth & Modernization
History of Indian Railways - the story of Growth & Modernization
 
Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..
 
Fuzzy logic method-based stress detector with blood pressure and body tempera...
Fuzzy logic method-based stress detector with blood pressure and body tempera...Fuzzy logic method-based stress detector with blood pressure and body tempera...
Fuzzy logic method-based stress detector with blood pressure and body tempera...
 

AVL Tree