SlideShare a Scribd company logo
1 of 48
Download to read offline
Instructor: Lilian de Greef
Quarter: Summer 2017
CSE 373: Data Structures and Algorithms
Lecture 10: AVL Trees
Today
• Announcements
• BSTs continued (this time, bringing
• buildTree
• Balance Conditions
• AVL Trees
• Tree rotations
Announcements
• Reminder: homework 3 due Friday
• Homework 2 grades should come out today
• Section
• Will especially go over material from today (it’s especially tricky)
• TAs can go over some of the tougher hw2 questions in section if you want/ask
Back to Binary Search Trees
buildTree for BST
Let’s consider buildTree (insert values starting from an empty tree)
Insert values 1, 2, 3, 4, 5, 6, 7, 8, 9 into an empty BST
• If inserted in given order,
what is the tree?
• What big-O runtime for
buildTree on this sorted input?
• Is inserting in the reverse order any better?
buildTree for BST
Insert values 1, 2, 3, 4, 5, 6, 7, 8, 9 into an empty BST
What we if could somehow re-arrange them
• median first, then left median, right median, etc.
5, 3, 7, 2, 1, 4, 8, 6, 9
• What tree does that give us?
• What big-O runtime?
Balancing Binary Search Trees
BST: Efficiency of Operations?
Problem:
Worst-case running time:
• find, insert, delete
• buildTree
How can we make a BST efficient?
Observation
Solution: Require a Balance Condition that
• When we build the tree, make sure it’s balanced.
• BUT…Balancing a tree only at build time is insufficient.
• We also need to also keep the tree balanced as we perform operations.
Potential Balance Conditions
• Left and right subtrees
• Left and right subtrees
Potential Balance Conditions
• Left and right subtrees
• Left and right subtrees
Potential Balance Conditions
Left and right subtrees
AVL Tree (Bonus material: etymology)
Invented by Georgy Adelson-Velsky and Evgenii Landis in 1962
The AVL Tree Data Structure
An AVL tree is a self-balancing binary search tree.
Structural properties
1. Binary tree property (same as BST)
2. Order property (same as for BST)
3. Balance condition:
balance of every node is between -1 and 1
where balance(node) = height(node.left) – height(node.right)
Result: Worst-case depth is
Example #1: Is this an AVL Tree?
Balance Condition:
balance of every node is between -1 and 1
where balance(node) =
height(node.left) – height(node.right)
11
1
8
4
6
10 12
7
Example #2: Is this an AVL Tree?
3
11
7
1
8
4
6
2
5
Balance Condition:
balance of every node is between -1 and 1
where balance(node) =
height(node.left) – height(node.right)
AVL Trees
Good News:
Because height of AVL tree is O(log(n)), then find
But as we insert and delete elements, we need to:
AVL Trees
20
9
2 15
5
10
30
17
7
0
0 0
0
1
1
2 2
3
AVL tree operations
• AVL find:
• Same as usual BST find
• AVL insert:
• AVL delete:
• The “easy way” is lazy deletion
• Otherwise, do the deletion and then check for several imbalance cases (we will skip
this)
First insert example
Insert(6)
Insert(3)
Insert(1)
Third insertion
What’s the only way to fix it?
Fix: Apply “Single Rotation”
• Single rotation: The basic operation we’ll use to rebalance
• Move child of unbalanced node into parent position
• Parent becomes the “other” child (always okay in a BST!)
• Other subtrees move in only way BST allows (we’ll see in generalized example)
3
1 6
6
3
AVL Property
violated at node 6
1
Tree Rotations: Generalized
Generalizing our examples…
30
9
2
23
5
12
7 10
18
4
1
Generalizing our examples…
30
9
2
23
5
12
7 10
18
4
1
E
C
A
Generalizing our examples…
5
12
12
E
C
A
Generalizing our examples…
b
d
Generalized Single Rotation
E
C
A
b
d
Generalized Single Rotation
A
C E
d
b
Single Rotations
(Figures by Melissa O’Neill, reprinted with her permission to Lilian)
AVL Tree insert (more specific):
1. Insert the new node as in our generic BST (a new leaf)
2. For each node on the path from the root to the new leaf, the
insertion may (or may not) have changed the node’s height
3. So after insertion in a subtree,
Case #1:
E
C
A
b
d
a’
(Figures by Melissa O’Neill, reprinted with her permission to Lilian)
Example #2 for left-left case: insert(16)
10
4
22
8
15
3 6
19
17 20
24
16
Case #2:
(Figures by Melissa O’Neill, reprinted with her permission to Lilian)
Example for right-right case: insert(26)
10
4
22
8
15
6
19
23 25
26
3
24 10
4 22
8
15
6 19 23
25
26
3
24
Case #3:
(Figures by Melissa O’Neill, reprinted with her permission to Lilian)
A Better Look at Case #3:
(Figures by Melissa O’Neill, reprinted with her permission to Lilian)
Case #3: Right-Left Case (after one rotation)
(Figures by Melissa O’Neill, reprinted with her permission to Lilian)
right rotate
A way to remember it:
Move d to grandparent’s position. Put everything else in their only legal positions for a BST.
Practice time! Example of Case #4
A)
50
10
80
30
60
10
30
42
80
50
60
10
30
50
80
42
60
10
30
50
80
42
60
42
10
30
80
60
50
B) C) D)
Which of the following
is the updated AVL tree
after inserting 42?
Starting with this
AVL tree:
(Extra space for your scratch-work)
Practice time! Example of Case #4
50
10
80
30
60 Which of the following
is the updated AVL tree
after inserting 42?
Starting with this
AVL tree:
What rotations did we do?
What’s the name of this case?
Insert, summarized
• Insert as in our generic BST
• Check back up path for imbalance, which will be 1 of 4 cases:
• Node’s left-left grandchild is too tall
• Node’s left-right grandchild is too tall
• Node’s right-left grandchild is too tall
• Node’s right-right grandchild is too tall
• Only occurs because
• After the appropriate single or double rotation, the smallest-unbalanced subtree
has the same height as before the insertion
• So all ancestors are now balanced
41
AVL Tree Efficiency
• Worst-case complexity of find:
• Worst-case complexity of insert:
• Worst-case complexity of buildTree:
Takes some more rotation action to handle delete…
Pros and Cons of AVL Trees
Arguments for AVL trees:
1. All operations logarithmic worst-case because trees are always balanced
2. Height balancing adds no more than a constant factor to the speed of
insert and delete
Arguments against AVL trees:
1. Difficult to program & debug [but done once in a library!]
2. More space for height field
3. Asymptotically faster but rebalancing takes a little time
4. If amortized logarithmic time is enough, use splay trees (also in the text,
not covered in this class)
AVL Tree Rotation Cheat-Sheet
(Just two of the four cases)
Single Rotations
(Figures by Melissa O’Neill, reprinted with her permission to Lilian)
Case #2: Left-Left Case
(Figures by Melissa O’Neill, reprinted with her permission to Lilian)
left rotate
Case #3: Right-Left Case (after two rotations)
(Figures by Melissa O’Neill, reprinted with her permission to Lilian)
right rotate
left rotate
A way to remember it:
Move d to grandparent’s position. Put everything else in their only legal positions for a BST.

More Related Content

Similar to Lecture 10 - AVL Trees.pdf

Similar to Lecture 10 - AVL Trees.pdf (20)

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 Data Structure
AVL Tree Data StructureAVL Tree Data Structure
AVL Tree Data Structure
 
avl.ppt
avl.pptavl.ppt
avl.ppt
 
avl.ppt
avl.pptavl.ppt
avl.ppt
 
lecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminologylecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminology
 
Av ltrees
Av ltreesAv ltrees
Av ltrees
 
AVL Tree.pptx
AVL Tree.pptxAVL Tree.pptx
AVL Tree.pptx
 
Avl trees
Avl treesAvl trees
Avl trees
 
Avl tree
Avl treeAvl tree
Avl tree
 
Avl trees final
Avl trees finalAvl trees final
Avl trees final
 
AVL Tree in Data Structure
AVL Tree in Data Structure AVL Tree in Data Structure
AVL Tree in Data Structure
 
Avltrees
AvltreesAvltrees
Avltrees
 
Data Structures 5
Data Structures 5Data Structures 5
Data Structures 5
 
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
 
Lecture 09 - Binary Search Trees.pptx mission Sk it
Lecture 09 - Binary Search Trees.pptx mission Sk itLecture 09 - Binary Search Trees.pptx mission Sk it
Lecture 09 - Binary Search Trees.pptx mission Sk it
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
 
Ie
IeIe
Ie
 
Data Structures 4
Data Structures 4Data Structures 4
Data Structures 4
 
C++ UNIT4.pptx
C++ UNIT4.pptxC++ UNIT4.pptx
C++ UNIT4.pptx
 
9 chapter4 trees_avl
9 chapter4 trees_avl9 chapter4 trees_avl
9 chapter4 trees_avl
 

Recently uploaded

(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Ravak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxRavak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxolyaivanovalion
 
Introduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxIntroduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxfirstjob4
 
Smarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptxSmarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptxolyaivanovalion
 
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAroojKhan71
 
Unveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystUnveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystSamantha Rae Coolbeth
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionfulawalesam
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130Suhani Kapoor
 
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptxBPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptxMohammedJunaid861692
 
Edukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxEdukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxolyaivanovalion
 
April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysismanisha194592
 
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfLars Albertsson
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptxAnupama Kate
 
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...Suhani Kapoor
 
Halmar dropshipping via API with DroFx
Halmar  dropshipping  via API with DroFxHalmar  dropshipping  via API with DroFx
Halmar dropshipping via API with DroFxolyaivanovalion
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfMarinCaroMartnezBerg
 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...Suhani Kapoor
 

Recently uploaded (20)

(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
 
Ravak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxRavak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptx
 
Introduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxIntroduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptx
 
Smarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptxSmarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptx
 
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
 
Unveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystUnveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data Analyst
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interaction
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
 
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptxBPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
 
Edukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxEdukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFx
 
April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysis
 
Sampling (random) method and Non random.ppt
Sampling (random) method and Non random.pptSampling (random) method and Non random.ppt
Sampling (random) method and Non random.ppt
 
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdf
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx
 
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
 
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
 
Halmar dropshipping via API with DroFx
Halmar  dropshipping  via API with DroFxHalmar  dropshipping  via API with DroFx
Halmar dropshipping via API with DroFx
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdf
 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
 

Lecture 10 - AVL Trees.pdf

  • 1. Instructor: Lilian de Greef Quarter: Summer 2017 CSE 373: Data Structures and Algorithms Lecture 10: AVL Trees
  • 2. Today • Announcements • BSTs continued (this time, bringing • buildTree • Balance Conditions • AVL Trees • Tree rotations
  • 3. Announcements • Reminder: homework 3 due Friday • Homework 2 grades should come out today • Section • Will especially go over material from today (it’s especially tricky) • TAs can go over some of the tougher hw2 questions in section if you want/ask
  • 4. Back to Binary Search Trees
  • 5. buildTree for BST Let’s consider buildTree (insert values starting from an empty tree) Insert values 1, 2, 3, 4, 5, 6, 7, 8, 9 into an empty BST • If inserted in given order, what is the tree? • What big-O runtime for buildTree on this sorted input? • Is inserting in the reverse order any better?
  • 6. buildTree for BST Insert values 1, 2, 3, 4, 5, 6, 7, 8, 9 into an empty BST What we if could somehow re-arrange them • median first, then left median, right median, etc. 5, 3, 7, 2, 1, 4, 8, 6, 9 • What tree does that give us? • What big-O runtime?
  • 8. BST: Efficiency of Operations? Problem: Worst-case running time: • find, insert, delete • buildTree
  • 9. How can we make a BST efficient? Observation Solution: Require a Balance Condition that • When we build the tree, make sure it’s balanced. • BUT…Balancing a tree only at build time is insufficient. • We also need to also keep the tree balanced as we perform operations.
  • 10. Potential Balance Conditions • Left and right subtrees • Left and right subtrees
  • 11. Potential Balance Conditions • Left and right subtrees • Left and right subtrees
  • 12. Potential Balance Conditions Left and right subtrees
  • 13. AVL Tree (Bonus material: etymology) Invented by Georgy Adelson-Velsky and Evgenii Landis in 1962
  • 14. The AVL Tree Data Structure An AVL tree is a self-balancing binary search tree. Structural properties 1. Binary tree property (same as BST) 2. Order property (same as for BST) 3. Balance condition: balance of every node is between -1 and 1 where balance(node) = height(node.left) – height(node.right) Result: Worst-case depth is
  • 15. Example #1: Is this an AVL Tree? Balance Condition: balance of every node is between -1 and 1 where balance(node) = height(node.left) – height(node.right) 11 1 8 4 6 10 12 7
  • 16. Example #2: Is this an AVL Tree? 3 11 7 1 8 4 6 2 5 Balance Condition: balance of every node is between -1 and 1 where balance(node) = height(node.left) – height(node.right)
  • 17. AVL Trees Good News: Because height of AVL tree is O(log(n)), then find But as we insert and delete elements, we need to:
  • 19. AVL tree operations • AVL find: • Same as usual BST find • AVL insert: • AVL delete: • The “easy way” is lazy deletion • Otherwise, do the deletion and then check for several imbalance cases (we will skip this)
  • 20. First insert example Insert(6) Insert(3) Insert(1) Third insertion What’s the only way to fix it?
  • 21. Fix: Apply “Single Rotation” • Single rotation: The basic operation we’ll use to rebalance • Move child of unbalanced node into parent position • Parent becomes the “other” child (always okay in a BST!) • Other subtrees move in only way BST allows (we’ll see in generalized example) 3 1 6 6 3 AVL Property violated at node 6 1
  • 29. Single Rotations (Figures by Melissa O’Neill, reprinted with her permission to Lilian)
  • 30. AVL Tree insert (more specific): 1. Insert the new node as in our generic BST (a new leaf) 2. For each node on the path from the root to the new leaf, the insertion may (or may not) have changed the node’s height 3. So after insertion in a subtree,
  • 31. Case #1: E C A b d a’ (Figures by Melissa O’Neill, reprinted with her permission to Lilian)
  • 32. Example #2 for left-left case: insert(16) 10 4 22 8 15 3 6 19 17 20 24 16
  • 33. Case #2: (Figures by Melissa O’Neill, reprinted with her permission to Lilian)
  • 34. Example for right-right case: insert(26) 10 4 22 8 15 6 19 23 25 26 3 24 10 4 22 8 15 6 19 23 25 26 3 24
  • 35. Case #3: (Figures by Melissa O’Neill, reprinted with her permission to Lilian)
  • 36. A Better Look at Case #3: (Figures by Melissa O’Neill, reprinted with her permission to Lilian)
  • 37. Case #3: Right-Left Case (after one rotation) (Figures by Melissa O’Neill, reprinted with her permission to Lilian) right rotate A way to remember it: Move d to grandparent’s position. Put everything else in their only legal positions for a BST.
  • 38. Practice time! Example of Case #4 A) 50 10 80 30 60 10 30 42 80 50 60 10 30 50 80 42 60 10 30 50 80 42 60 42 10 30 80 60 50 B) C) D) Which of the following is the updated AVL tree after inserting 42? Starting with this AVL tree:
  • 39. (Extra space for your scratch-work)
  • 40. Practice time! Example of Case #4 50 10 80 30 60 Which of the following is the updated AVL tree after inserting 42? Starting with this AVL tree: What rotations did we do? What’s the name of this case?
  • 41. Insert, summarized • Insert as in our generic BST • Check back up path for imbalance, which will be 1 of 4 cases: • Node’s left-left grandchild is too tall • Node’s left-right grandchild is too tall • Node’s right-left grandchild is too tall • Node’s right-right grandchild is too tall • Only occurs because • After the appropriate single or double rotation, the smallest-unbalanced subtree has the same height as before the insertion • So all ancestors are now balanced 41
  • 42. AVL Tree Efficiency • Worst-case complexity of find: • Worst-case complexity of insert: • Worst-case complexity of buildTree: Takes some more rotation action to handle delete…
  • 43. Pros and Cons of AVL Trees Arguments for AVL trees: 1. All operations logarithmic worst-case because trees are always balanced 2. Height balancing adds no more than a constant factor to the speed of insert and delete Arguments against AVL trees: 1. Difficult to program & debug [but done once in a library!] 2. More space for height field 3. Asymptotically faster but rebalancing takes a little time 4. If amortized logarithmic time is enough, use splay trees (also in the text, not covered in this class)
  • 44.
  • 45. AVL Tree Rotation Cheat-Sheet (Just two of the four cases)
  • 46. Single Rotations (Figures by Melissa O’Neill, reprinted with her permission to Lilian)
  • 47. Case #2: Left-Left Case (Figures by Melissa O’Neill, reprinted with her permission to Lilian) left rotate
  • 48. Case #3: Right-Left Case (after two rotations) (Figures by Melissa O’Neill, reprinted with her permission to Lilian) right rotate left rotate A way to remember it: Move d to grandparent’s position. Put everything else in their only legal positions for a BST.