SlideShare a Scribd company logo
1 of 15
AVLTREE
(Adelson-Velsky and Landis)
OR
SELF-BALANCINGBINARY SEARCHTREE
OR
HEIGHTBALANCEDTREE
AVL Tree
AVL Trees
• AVL tree is a self-balancing binary search tree in which the heights
of the two sub-trees of a node may differ by at most one.
• Because of this property, AVL tree is also known as a height-
balanced tree.
• The key advantage of using an AVL tree is that it takes O(logn) time
to perform search, insertion and deletion operations in average
case as well as worst case (because the height of the tree is limited
to O(logn)).
• The structure of an AVL tree is same as that of a binary search tree
but with a little difference. In its structure, it stores an additional
variable called the BalanceFactor.
AVL Tree
AVL Trees
• The balance factor of a node is calculated by subtracting the height
of its right sub-tree from the height of its left sub-tree.
Balance factor = Height (left sub-tree) – Height (right sub-tree)
• A binary search tree in which every node has a balance factor of -1, 0
or 1 is said to be height balanced. A node with any other balance
factor is considered to be unbalanced and requires rebalancing.
• If the balance factor of a node is 1, then it means that the left sub-
tree of the tree is one level higher than that of the right sub-tree.
Such a tree is called Left-heavy tree.
• If the balance factor of a node is 0, then it means that the height of
the left sub-tree is equal to the height of its right sub-tree.
• If the balance factor of a node is -1, then it means that the left sub-
tree of the tree is one level lower than that of the right sub-tree.
Such a tree is called Right-heavy tree.
AVL Tree
AVL Trees
45
63
36
27
18
39 54 72
0
0
0
1
0
1
0
1
45
63
36
27
70
39 54
72
1
0
0
0
-1
-1
0
0
45
63
27 39
54 72
0
0
0
0
0
0
0
36
Left heavy AVL tree
Right heavy AVL tree
AVL Tree
Various operation can be performed on AVL Tree
• Search
• Insertion
• Deletion
AVL Tree
Searching for a Node in an AVL Tree
• Searching in an AVL tree is performed exactly the same way as it is
performed in a binary search tree.
• Because of the height-balancing of the tree, the search operation
takes O(log n) time to complete.
• Since the operation does not modify the structure of the tree, no
special provisions need to be taken.
Inserting a Node in an AVL Tree
• Since an AVL tree is also a variant of binary search tree, insertion is
also done in the same way as it is done in case of a binary search
tree.
• Like in binary search tree, the new node is always inserted as the
leaf node. But the step of insertion is usually followed by an
additional step of rotation.
• Rotation is done to restore the balance of the tree. However, if
insertion of the new node does not disturb the balance factor, that
is, if the balance factor of every node is still -1, 0 or 1, then
rotations are not needed.
Inserting a Node in an AVL Tree
• During insertion, the new node is inserted as the leaf node, so it will
always have balance factor equal to zero.
• The nodes whose balance factors will change are those which lie on
the path between the root of the tree and the newly inserted node.
• The possible changes which may take place in any node on the path
are as follows:
 Initially the node was either left or right heavy and after insertion has
become balanced.
 Initially the node was balanced and after insertion has become either
left or right heavy.
 Initially the node was heavy (either left or right) and the new node has
been inserted in the heavy sub-tree thereby creating an unbalanced
sub-tree. Such a node is said to be a critical node.
AVL Tree Rotations
• Rotation is the process of moving the nodes to either left or right
to make tree balanced.
• To perform rotation, our first work is to find the critical node.
• Critical node is the nearest ancestor node on the path from the root to the
inserted node whose balance factor is neither -1, 0 nor 1.
• The second task is to determine which type of rotation has to be done.
• There are four types of rebalancing rotations
LL rotation: the new node is inserted in the left sub-tree of
the left child of the critical node
RR rotation: the new node is inserted in the right sub-tree
of the right child of the critical node
LR rotation: the new node is inserted in the right sub-tree
of the left child of the critical node
RL rotation: the new node is inserted in the left sub-tree
of the right child of the critical node
AVL Tree Rotations
Deleting a Node from an AVL Tree
• Deletion of a node in an AVL tree is similar to that of binary search trees.
• But deletion may disturb the AVLness of the tree, so to re-balance the
AVL tree we need to perform rotations.
• There are two classes of rotation that can be performed on an AVL tree
after deleting a given node: R rotation and L rotation.
• If the node to be deleted is present in the left sub-tree of the critical
node, then L rotation is applied else if node is in the right sub-tree, R
rotation is performed.
• Further there are three categories of L and R rotations. The variations of
L rotation are: L-1, L0 and L1 rotation. Correspondingly for R rotation,
there are R0, R-1 and R1 rotations.
Deleting a Node from an AVL Tree
R0 Rotation
 Let B be the root of the left or right sub-tree of A (critical node).
 R0 rotation is applied if the balance factor of B is 0.
 Consider the AVL tree given below and delete 72 from it.
45
63
36
27
39
72
-1
1
1
-1
0
18
0
40 0
0
45
63
36
27 39
0
2
1
-1
0
18 40
0 0
36
45
27
18
39
1
-1
0
-1
1
40
63
0
0
Deleting a Node from an AVL Tree
R1 Rotation
 Let B be the root of the left or right sub-tree of the critical node.
 R1 rotation is applied if the balance factor of B is 1.
 Consider the AVL tree given below and delete 72 from it.
45
63
36
27
39 72
1
1
1
0
1
18
0
0
45
63
36
27 39
0
2
1
0
1
18
0
36
45
27
18 39
0
0
0 0
1
63
0
Deleting a Node from an AVL Tree
R-1Rotation
 Let B be the root of the left or right sub-tree of the critical node.
 R-1 rotation is applied if the balance factor of B is -1.
 Consider the AVL tree given below and delete 72 from it.
45
63
36
27
39
-1
1
0 0
-1
37 41
0 0
72
0
45
63
36
27
39
0
2
0 0
-1
37 41
0 0
39
45
36
27 37 41
1
1
1 0
1
0
63
0
3-avl-tree.ppt

More Related Content

Similar to 3-avl-tree.ppt

Similar to 3-avl-tree.ppt (20)

Algorithm presentation
Algorithm presentationAlgorithm presentation
Algorithm presentation
 
Avl tree
Avl treeAvl tree
Avl tree
 
AVL_Trees using DSA concepts and how to do
AVL_Trees using DSA concepts and how to doAVL_Trees using DSA concepts and how to do
AVL_Trees using DSA concepts and how to do
 
Avltrees
AvltreesAvltrees
Avltrees
 
4. avl
4. avl4. avl
4. avl
 
Avl trees 2
Avl trees 2Avl trees 2
Avl trees 2
 
AVL TREE PREPARED BY M V BRAHMANANDA REDDY
AVL TREE PREPARED BY M V BRAHMANANDA REDDYAVL TREE PREPARED BY M V BRAHMANANDA REDDY
AVL TREE PREPARED BY M V BRAHMANANDA REDDY
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
 
Avl tree
Avl treeAvl tree
Avl tree
 
Study about AVL Tree & Operations
Study about AVL Tree & OperationsStudy about AVL Tree & Operations
Study about AVL Tree & Operations
 
AVL Tree Data Structure
AVL Tree Data StructureAVL Tree Data Structure
AVL Tree Data Structure
 
Avl trees
Avl treesAvl trees
Avl trees
 
AVL Trees.pptx DATA STRUCTURES AND ALGORITHMS
AVL Trees.pptx DATA STRUCTURES AND ALGORITHMSAVL Trees.pptx DATA STRUCTURES AND ALGORITHMS
AVL Trees.pptx DATA STRUCTURES AND ALGORITHMS
 
Avl tree
Avl treeAvl tree
Avl tree
 
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
 
Avl tree detailed
Avl tree detailedAvl tree detailed
Avl tree detailed
 
Avl tree
Avl treeAvl tree
Avl tree
 
Design data Analysis Avl Trees.pptx by piyush sir
Design data Analysis Avl Trees.pptx by piyush sirDesign data Analysis Avl Trees.pptx by piyush sir
Design data Analysis Avl Trees.pptx by piyush sir
 
data structures
data structuresdata structures
data structures
 
Avl tree-hash-table
Avl tree-hash-tableAvl tree-hash-table
Avl tree-hash-table
 

Recently uploaded

UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...RajaP95
 
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
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
(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
 
(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
 
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
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...ranjana rawat
 
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
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 

Recently uploaded (20)

UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
 
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
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
(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...
 
(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 Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
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
 
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
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
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
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 

3-avl-tree.ppt

  • 2. AVL Tree AVL Trees • AVL tree is a self-balancing binary search tree in which the heights of the two sub-trees of a node may differ by at most one. • Because of this property, AVL tree is also known as a height- balanced tree. • The key advantage of using an AVL tree is that it takes O(logn) time to perform search, insertion and deletion operations in average case as well as worst case (because the height of the tree is limited to O(logn)). • The structure of an AVL tree is same as that of a binary search tree but with a little difference. In its structure, it stores an additional variable called the BalanceFactor.
  • 3. AVL Tree AVL Trees • The balance factor of a node is calculated by subtracting the height of its right sub-tree from the height of its left sub-tree. Balance factor = Height (left sub-tree) – Height (right sub-tree) • A binary search tree in which every node has a balance factor of -1, 0 or 1 is said to be height balanced. A node with any other balance factor is considered to be unbalanced and requires rebalancing. • If the balance factor of a node is 1, then it means that the left sub- tree of the tree is one level higher than that of the right sub-tree. Such a tree is called Left-heavy tree. • If the balance factor of a node is 0, then it means that the height of the left sub-tree is equal to the height of its right sub-tree. • If the balance factor of a node is -1, then it means that the left sub- tree of the tree is one level lower than that of the right sub-tree. Such a tree is called Right-heavy tree.
  • 4. AVL Tree AVL Trees 45 63 36 27 18 39 54 72 0 0 0 1 0 1 0 1 45 63 36 27 70 39 54 72 1 0 0 0 -1 -1 0 0 45 63 27 39 54 72 0 0 0 0 0 0 0 36 Left heavy AVL tree Right heavy AVL tree
  • 5. AVL Tree Various operation can be performed on AVL Tree • Search • Insertion • Deletion
  • 6. AVL Tree Searching for a Node in an AVL Tree • Searching in an AVL tree is performed exactly the same way as it is performed in a binary search tree. • Because of the height-balancing of the tree, the search operation takes O(log n) time to complete. • Since the operation does not modify the structure of the tree, no special provisions need to be taken.
  • 7. Inserting a Node in an AVL Tree • Since an AVL tree is also a variant of binary search tree, insertion is also done in the same way as it is done in case of a binary search tree. • Like in binary search tree, the new node is always inserted as the leaf node. But the step of insertion is usually followed by an additional step of rotation. • Rotation is done to restore the balance of the tree. However, if insertion of the new node does not disturb the balance factor, that is, if the balance factor of every node is still -1, 0 or 1, then rotations are not needed.
  • 8. Inserting a Node in an AVL Tree • During insertion, the new node is inserted as the leaf node, so it will always have balance factor equal to zero. • The nodes whose balance factors will change are those which lie on the path between the root of the tree and the newly inserted node. • The possible changes which may take place in any node on the path are as follows:  Initially the node was either left or right heavy and after insertion has become balanced.  Initially the node was balanced and after insertion has become either left or right heavy.  Initially the node was heavy (either left or right) and the new node has been inserted in the heavy sub-tree thereby creating an unbalanced sub-tree. Such a node is said to be a critical node.
  • 9. AVL Tree Rotations • Rotation is the process of moving the nodes to either left or right to make tree balanced. • To perform rotation, our first work is to find the critical node. • Critical node is the nearest ancestor node on the path from the root to the inserted node whose balance factor is neither -1, 0 nor 1. • The second task is to determine which type of rotation has to be done. • There are four types of rebalancing rotations
  • 10. LL rotation: the new node is inserted in the left sub-tree of the left child of the critical node RR rotation: the new node is inserted in the right sub-tree of the right child of the critical node LR rotation: the new node is inserted in the right sub-tree of the left child of the critical node RL rotation: the new node is inserted in the left sub-tree of the right child of the critical node AVL Tree Rotations
  • 11. Deleting a Node from an AVL Tree • Deletion of a node in an AVL tree is similar to that of binary search trees. • But deletion may disturb the AVLness of the tree, so to re-balance the AVL tree we need to perform rotations. • There are two classes of rotation that can be performed on an AVL tree after deleting a given node: R rotation and L rotation. • If the node to be deleted is present in the left sub-tree of the critical node, then L rotation is applied else if node is in the right sub-tree, R rotation is performed. • Further there are three categories of L and R rotations. The variations of L rotation are: L-1, L0 and L1 rotation. Correspondingly for R rotation, there are R0, R-1 and R1 rotations.
  • 12. Deleting a Node from an AVL Tree R0 Rotation  Let B be the root of the left or right sub-tree of A (critical node).  R0 rotation is applied if the balance factor of B is 0.  Consider the AVL tree given below and delete 72 from it. 45 63 36 27 39 72 -1 1 1 -1 0 18 0 40 0 0 45 63 36 27 39 0 2 1 -1 0 18 40 0 0 36 45 27 18 39 1 -1 0 -1 1 40 63 0 0
  • 13. Deleting a Node from an AVL Tree R1 Rotation  Let B be the root of the left or right sub-tree of the critical node.  R1 rotation is applied if the balance factor of B is 1.  Consider the AVL tree given below and delete 72 from it. 45 63 36 27 39 72 1 1 1 0 1 18 0 0 45 63 36 27 39 0 2 1 0 1 18 0 36 45 27 18 39 0 0 0 0 1 63 0
  • 14. Deleting a Node from an AVL Tree R-1Rotation  Let B be the root of the left or right sub-tree of the critical node.  R-1 rotation is applied if the balance factor of B is -1.  Consider the AVL tree given below and delete 72 from it. 45 63 36 27 39 -1 1 0 0 -1 37 41 0 0 72 0 45 63 36 27 39 0 2 0 0 -1 37 41 0 0 39 45 36 27 37 41 1 1 1 0 1 0 63 0