SlideShare a Scribd company logo
1 of 15
Download to read offline
AVL trees and rotations
/
 Operations (insert, delete, search) are
O(height)
 Tree height is O(log n) if perfectly
balanced
◦ But maintaining perfect balance is O(n)
 Height-balanced trees are still O(log n)
◦ For T with height h, N(T) ≤ Fib(h+3) – 1
◦ So H < 1.44 log (N+2) – 1.328 *
 AVL (Adelson-Velskii and Landis) trees
maintain height-balance using
rotations
 Are rotations O(log n)? We’ll see…
Q1
Different representations for / =  :
 Just two bits in a low-level language
 Enum in a higher-level language
or/ = or
 Assume tree is height-balanced before
insertion
 Insert as usual for a BST
 Move up from the newly inserted node
to the lowest “unbalanced” node (if any)
◦ Use the balance code to detect unbalance -
how?
 Do an appropriate rotation to balance
the sub-tree rooted at this unbalanced
node
/
 For example, a single left rotation:
 Two basic cases
◦ “See saw” case:
 Too-tall sub-tree is on the outside
 So tip the see saw so it’s level
◦ “Suck in your gut” case:
 Too-tall sub-tree is in the middle
 Pull its root up a level
Diagrams are from Data Structures by E.M. Reingold and W.J. Hansen
Unbalanced node
Middle sub-tree
attaches to lower node
of the “see saw”
Q2-3
Weiss calls this “right-left double rotation”
Unbalanced node
Pulled up
Split between the
nodes pushed down
Q4-5
 Write the method:
 static BalancedBinaryNode singleRotateLeft (
BalancedBinaryNode parent, /* A */
BalancedBinaryNode child /* B */ ) {
}
 Returns a reference to the new root of this subtree.
 Don’t forget to set the balanceCode fields of the nodes.
Q6
 Write the method:
 BalancedBinaryNode doubleRotateRight (
BalancedBinaryNode parent, /* A */
BalancedBinaryNode child, /* C */
BalancedBinaryNode grandChild /* B */ ) {
}
 Returns a reference to the new root of this subtree.
 Rotation is mirror image of double rotation from an
earlier slide
 Both kinds of rotation leave height the same
as before the insertion!
 Is insertion plus rotation cost really O(log N)?
Q7-10
Insertion/deletion
in AVL Tree: O(log n)
Find the imbalance point (if any): O(log n)
Single or double rotation: O(1)
in deletion case, may have
to do O(log N) rotations
Total work: O(log n)
Depends on the first two links in the path from
the lowest node that has the imbalance (A)
down to the newly-inserted node.
First link
(down from A)
Second link
(down from A's
child)
Rotation type
(rotate "around
A's position")
Left Left Single right
Left Right Double right
Right Right Single left
Right Left Double left
Insert HA into the tree, then DA, then O.
Delete G from the original tree, then I, J, V.
 Start with an empty AVL tree.
 Add elements in the following order; do
the appropriate rotations when needed.
◦ 1 2 3 4 5 6 11 13 12 10 9 8 7
 How should we rebalance if each of the
following sequences is deleted from the
above tree?
◦ ( 10 9 7 8 ) ( 13 ) ( 1 5 )
◦ For each of the three sequences, start with the
original 13-element tree. E.g. when deleting
13, assume 10 9 8 7 are still in the tree.
Work with your Doublets partner.
When you finish, work on Doublets or WA5.
Or write the rotateDoubleRight code from a previous slide
avl insertion-rotation

More Related Content

Similar to avl insertion-rotation

Similar to avl insertion-rotation (20)

Avl tree
Avl treeAvl tree
Avl tree
 
Avl trees
Avl treesAvl trees
Avl trees
 
AVL-TREE.ppt
AVL-TREE.pptAVL-TREE.ppt
AVL-TREE.ppt
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
 
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 detailed
Avl tree detailedAvl tree detailed
Avl tree detailed
 
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
 
Avl tree
Avl treeAvl tree
Avl tree
 
Binary trees1
Binary trees1Binary trees1
Binary trees1
 
Avl trees final
Avl trees finalAvl trees final
Avl trees final
 
Ie
IeIe
Ie
 
Lecture3
Lecture3Lecture3
Lecture3
 
AVL Tree Data Structure
AVL Tree Data StructureAVL Tree Data Structure
AVL Tree Data Structure
 
Adelson velskii Landis rotations based on
Adelson velskii Landis rotations based onAdelson velskii Landis rotations based on
Adelson velskii Landis rotations based on
 
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
 
AVL Tree.pptx
AVL Tree.pptxAVL Tree.pptx
AVL Tree.pptx
 
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
 
avl.ppt
avl.pptavl.ppt
avl.ppt
 
avl.ppt
avl.pptavl.ppt
avl.ppt
 
4.10.AVLTrees[1].ppt
4.10.AVLTrees[1].ppt4.10.AVLTrees[1].ppt
4.10.AVLTrees[1].ppt
 

More from Xad Kuain

History evaluation
History evaluationHistory evaluation
History evaluationXad Kuain
 
Computer and programming language
Computer and programming languageComputer and programming language
Computer and programming languageXad Kuain
 
How to build a lamp server-sample
How to build a lamp server-sampleHow to build a lamp server-sample
How to build a lamp server-sampleXad Kuain
 
Oracle 11g Database Administration
Oracle 11g Database Administration Oracle 11g Database Administration
Oracle 11g Database Administration Xad Kuain
 
Computer Graphics & linear Algebra
Computer Graphics & linear Algebra Computer Graphics & linear Algebra
Computer Graphics & linear Algebra Xad Kuain
 
Quality assurance by Sadquain
Quality assurance by Sadquain Quality assurance by Sadquain
Quality assurance by Sadquain Xad Kuain
 
C programming
C programmingC programming
C programmingXad Kuain
 

More from Xad Kuain (7)

History evaluation
History evaluationHistory evaluation
History evaluation
 
Computer and programming language
Computer and programming languageComputer and programming language
Computer and programming language
 
How to build a lamp server-sample
How to build a lamp server-sampleHow to build a lamp server-sample
How to build a lamp server-sample
 
Oracle 11g Database Administration
Oracle 11g Database Administration Oracle 11g Database Administration
Oracle 11g Database Administration
 
Computer Graphics & linear Algebra
Computer Graphics & linear Algebra Computer Graphics & linear Algebra
Computer Graphics & linear Algebra
 
Quality assurance by Sadquain
Quality assurance by Sadquain Quality assurance by Sadquain
Quality assurance by Sadquain
 
C programming
C programmingC programming
C programming
 

Recently uploaded

Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningVitsRangannavar
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 

Recently uploaded (20)

Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learning
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 

avl insertion-rotation

  • 1. AVL trees and rotations /
  • 2.  Operations (insert, delete, search) are O(height)  Tree height is O(log n) if perfectly balanced ◦ But maintaining perfect balance is O(n)  Height-balanced trees are still O(log n) ◦ For T with height h, N(T) ≤ Fib(h+3) – 1 ◦ So H < 1.44 log (N+2) – 1.328 *  AVL (Adelson-Velskii and Landis) trees maintain height-balance using rotations  Are rotations O(log n)? We’ll see… Q1
  • 3. Different representations for / = :  Just two bits in a low-level language  Enum in a higher-level language or/ = or
  • 4.  Assume tree is height-balanced before insertion  Insert as usual for a BST  Move up from the newly inserted node to the lowest “unbalanced” node (if any) ◦ Use the balance code to detect unbalance - how?  Do an appropriate rotation to balance the sub-tree rooted at this unbalanced node /
  • 5.  For example, a single left rotation:
  • 6.  Two basic cases ◦ “See saw” case:  Too-tall sub-tree is on the outside  So tip the see saw so it’s level ◦ “Suck in your gut” case:  Too-tall sub-tree is in the middle  Pull its root up a level
  • 7. Diagrams are from Data Structures by E.M. Reingold and W.J. Hansen Unbalanced node Middle sub-tree attaches to lower node of the “see saw” Q2-3
  • 8. Weiss calls this “right-left double rotation” Unbalanced node Pulled up Split between the nodes pushed down Q4-5
  • 9.  Write the method:  static BalancedBinaryNode singleRotateLeft ( BalancedBinaryNode parent, /* A */ BalancedBinaryNode child /* B */ ) { }  Returns a reference to the new root of this subtree.  Don’t forget to set the balanceCode fields of the nodes. Q6
  • 10.  Write the method:  BalancedBinaryNode doubleRotateRight ( BalancedBinaryNode parent, /* A */ BalancedBinaryNode child, /* C */ BalancedBinaryNode grandChild /* B */ ) { }  Returns a reference to the new root of this subtree.  Rotation is mirror image of double rotation from an earlier slide
  • 11.  Both kinds of rotation leave height the same as before the insertion!  Is insertion plus rotation cost really O(log N)? Q7-10 Insertion/deletion in AVL Tree: O(log n) Find the imbalance point (if any): O(log n) Single or double rotation: O(1) in deletion case, may have to do O(log N) rotations Total work: O(log n)
  • 12. Depends on the first two links in the path from the lowest node that has the imbalance (A) down to the newly-inserted node. First link (down from A) Second link (down from A's child) Rotation type (rotate "around A's position") Left Left Single right Left Right Double right Right Right Single left Right Left Double left
  • 13. Insert HA into the tree, then DA, then O. Delete G from the original tree, then I, J, V.
  • 14.  Start with an empty AVL tree.  Add elements in the following order; do the appropriate rotations when needed. ◦ 1 2 3 4 5 6 11 13 12 10 9 8 7  How should we rebalance if each of the following sequences is deleted from the above tree? ◦ ( 10 9 7 8 ) ( 13 ) ( 1 5 ) ◦ For each of the three sequences, start with the original 13-element tree. E.g. when deleting 13, assume 10 9 8 7 are still in the tree. Work with your Doublets partner. When you finish, work on Doublets or WA5. Or write the rotateDoubleRight code from a previous slide