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

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 (6)

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

APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 

Recently uploaded (20)

APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 

AVL Tree