SlideShare a Scribd company logo
Data Structures and
Algorithms
Week 5: AVL Trees
Ferdin Joe John Joseph, PhD
Faculty of Information Technology
Thai-Nichi Institute of Technology, Bangkok
Week 5
• AVL Trees
• Implementation in Java
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
2
AVL Trees
• Named after Adelson, Velski and EM Landis
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
3
Balance
Balance == height(left subtree) - height(right subtree)
• zero everywhere ⇒ perfectly balanced
• small everywhere ⇒ balanced enough
Balance between -1 and 1 everywhere
⇒ maximum height of ~1.44 log n
t
5
7
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
4
AVL Tree
Dictionary Data Structure
4
121062
115
8
14137 9
Binary search tree properties
• binary tree property
• search tree property
Balance property
• balance of every node is:
-1 ≤ b ≤ 1
15
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
5
An AVL Tree
15
92 12
5
10
20
17
0
0
100
1 2
3 10
3
data
height
children
30
0
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
6
Not AVL Trees
15
12
5
10
20
17
0
10
0 2
3
30
0
15
10
20
0
1
2 (-1)-1 = -2
0-2 = -2
Note: height(empty tree) == -1
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
7
Good Insert Case:
Balance Preserved
Good case: insert middle, then small,then tall
Insert(middle)
Insert(small)
Insert(tall)
M
S T
00
1
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
8
Bad Insert Case #1:
Left-Left or Right-Right Imbalance
Insert(small)
Insert(middle)
Insert(tall)
T
M
S
0
1
2
BC#1 Imbalance caused by either:
 Insert into left child’s left subtree
 Insert into right child’s right subtree
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
9
Single Rotation
T
M
S
0
1
2
M
S T
00
1
Basic operation used in AVL trees:
A right child could legally have its parent as its left child.
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
10
General Bad Case #1
a
X
Y
b
Z
h h - 1
h + 1 h - 1
h + 2
a
X
Y
b
Z
h-1 h - 1
h h - 1
h + 1
Note: imbalance is left-left
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
11
Single Rotation
Fixes Case #1 Imbalance
• Height of left subtree same as it was before insert!
• Height of all ancestors unchanged
• We can stop here!
a
X
Y
b
Z
a
XY
b
Z
h h - 1
h + 1 h - 1
h + 2
h
h - 1
h
h - 1
h + 1
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
12
Bad Insert Case #2:
Left-Right or Right-Left Imbalance
Insert(small)
Insert(tall)
Insert(middle)
M
T
S
0
1
2
Will a single rotation fix this?
BC#2 Imbalance caused by either:
 Insert into left child’s right subtree
 Insert into right child’s left subtree
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
13
Double Rotation
M
S T
00
1
M
T
S
0
1
2
T
M
S
0
1
2
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
14
General Bad Case #2
a
X
b
Z
h
h + 1 h - 1
h + 2
a
X
b
Z
h-1 h - 1
h h - 1
h + 1
Note: imbalance is left-right
h-1
Y
Y
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
15
Double Rotation
Fixes Case #2 Imbalance
Initially: insert into either X or Y unbalances tree (root balance goes to 2 or -2)
“Zig zag” to pull up c – restores root height to h+1, left subtree height to h
a
Z
b
W
c
X Y
a
Z
b
W
c
X Y
h
h - 1?
h - 1
h - 1
h + 2
h + 1
h - 1h - 1
h
h + 1
h
h - 1?
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
16
AVL Insert Algorithm
• Find spot for value
• Hang new node
• Search back up looking for imbalance
• If there is an imbalance:
case #1: Perform single rotation
case #2: Perform double rotation
• Done!
(There can only be one imbalance!)
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
17
Easy Insert
2092
155
10
3017
Insert(3)
12
0
0
100
1 2
3
0
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
18
Hard Insert (Bad Case #1)
2092
155
10
3017
Insert(33)
3
12
1
0
100
2 2
3
00
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
19
Single Rotation
2092
155
10
30173
12
33
1
0
200
2 3
3
10
0
3092
205
10
333
15
1
0
110
2 2
3
00
1712
0
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
20
Hard Insert (Bad Case #2)
Insert(18)
2092
155
10
30173
12
1
0
100
2 2
3
00
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
21
Single Rotation (oops!)
2092
155
10
30173
12
1
1
200
2 3
3
00
3092
205
10
3
15
1
1
020
2 3
3
0
1712
0
18
0
18
0
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
22
Double Rotation (Step #1)
2092
155
10
30173
12
1
1
200
2 3
3
00
18
0
1792
155
10
203
12
1 200
2 3
3
10
30
0
Look familiar? 18
0
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
23
Double Rotation (Step #2)
1792
155
10
203
12
1 200
2 3
3
10
30
0
18
0
2092
175
10
303
15
1
0
110
2 2
3
00
12
0
18
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
24
AVL Insert Algorithm Revisited
• Recursive
1. Search downward for
spot
2. Insert node
3. Unwind stack,
correcting heights
a. If imbalance #1,
single rotate
b. If imbalance #2,
double rotate
• Iterative
1. Search downward for
spot, stacking
parent nodes
2. Insert node
3. Unwind stack,
correcting heights
a. If imbalance #1,
single rotate and
exit
b. If imbalance #2,
double rotate and
exit
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
25
Single Rotation Code
void RotateRight(Node *& root) {
Node * temp = root->right;
root->right = temp->left;
temp->left = root;
root->height =
max(root->right->height,
root->left->height) + 1;
temp->height =
max(temp->right->height,
temp->left->height) + 1;
root = temp;
}
X
Y
Z
root
temp
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
26
Double Rotation Code
void DoubleRotateRight(Node *& root) {
RotateLeft(root->right);
RotateRight(root);
}
a
Z
b
W
c
XY
a
Z
c
b
X
Y
W
First Rotation
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
27
Double Rotation Completed
a
Z
c
b
X
Y
W
a
Z
c
b
XY
W
First Rotation Second Rotation
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
28
Deletion: Really Easy Case
2092
155
10
30173
12
1
0
100
2 2
3
00
Delete(17)
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
29
Deletion: Pretty Easy Case
2092
155
10
30173
12
1
0
100
2 2
3
00
Delete(15)
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
30
Deletion: Pretty Easy Case (cont.)
2092
175
10
303
12
1 100
2 2
3
00
Delete(15)
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
31
Deletion (Hard Case #1)
2092
175
10
303
12
1 100
2 2
3
00
Delete(12)
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
32
Single Rotation on Deletion
2092
175
10
303
1 10
2 2
3
00
3092
205
10
17
3
1 00
2 1
3
0
0
Deletion can differ from insertion – How?
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
33
Deletion (Hard Case)
Delete(9)
2092
175
10
303
12
1 220
2 3
4
0
33
15
13
0 0
1
0
20
30
12
33
15
13
1
0 0
11
0
18
0
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
34
Double Rotation on Deletion
2
3
0
202
175
10
30
12
1 22
2 3
4
33
15
13
1
0 0
1
11
0
18
0
2052
173
10
30
12
0 220
1 3
4
33
15
13
1
0 0
1
11
0
18
00
Not
finished!
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
35
Deletion with Propagation
We get to choose whether
to single or double rotate!
2052
173
10
30
12
0 220
1 3
4
33
15
13
1
0 0
1
11
0
18
0
What different about this case?
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
36
Propagated Single Rotation
0
30
20
17
33
12
15
13
1
0
52
3
10
4
3 2
1 2 1
0 0 0
11
0
2052
173
10
30
12
0 220
1 3
4
33
15
13
1
0
1
11
0
18
0
18
0
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
37
Propagated Double Rotation
0
17
12
11
52
3
10
4
2 3
1 0
0 0
2052
173
10
30
12
0 220
1 3
4
33
15
13
1
0
1
11
0
18
0
15
1
0
20
30
33
1
18
0
13
0
2
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
38
AVL Deletion Algorithm
• Recursive
1. Search downward for
node
2. Delete node
3. Unwind, correcting
heights as we go
a. If imbalance #1,
single rotate
b. If imbalance #2
(or don’t care),
double rotate
• Iterative
1. Search downward for
node, stacking
parent nodes
2. Delete node
3. Unwind stack,
correcting heights
a. If imbalance #1,
single rotate
b. If imbalance #2
(or don’t care)
double rotate
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
39
Assignment
• Write Java code to perform AVL tree based on the
Fibonacci series of length and elements as user
input.
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
40
Next Week
Red Black Trees
Graph Theory
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
41

More Related Content

PDF
Data Structures and Algorithm - Week 8 - Minimum Spanning Trees
PDF
Data Structures and Algorithm - Week 6 - Red Black Trees
PDF
Data Structures and Algorithm - Week 3 - Stacks and Queues
PDF
Data Structures and Algorithm - Week 4 - Trees, Binary Trees
PDF
Data Structures and Algorithm - Week 11 - Algorithm Analysis
PDF
Data Structures and Algorithm - Week 9 - Search Algorithms
PDF
Week 1 - Data Structures and Algorithms
PDF
Week 2 - Data Structures and Algorithms
Data Structures and Algorithm - Week 8 - Minimum Spanning Trees
Data Structures and Algorithm - Week 6 - Red Black Trees
Data Structures and Algorithm - Week 3 - Stacks and Queues
Data Structures and Algorithm - Week 4 - Trees, Binary Trees
Data Structures and Algorithm - Week 11 - Algorithm Analysis
Data Structures and Algorithm - Week 9 - Search Algorithms
Week 1 - Data Structures and Algorithms
Week 2 - Data Structures and Algorithms

What's hot (20)

PDF
Recommendation algorithm using reinforcement learning
PDF
Introduction to Exploratory Data Analysis with the sci-analysis Python Package
PDF
Test for AI model
PPTX
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
PDF
林守德/Practical Issues in Machine Learning
PDF
[系列活動] Machine Learning 機器學習課程
PDF
Tracking the tracker: Time Series Analysis in Python from First Principles
PPTX
8. Graph - Data Structures using C++ by Varsha Patil
PDF
Kaggle Days Paris - Alberto Danese - ML Interpretability
PPTX
[SIGIR17] Learning to Rank Using Localized Geometric Mean Metrics
PDF
Machine Learning and Model-Based Optimization for Heterogeneous Catalyst Desi...
PDF
Data structures algorithms_tutorial
PDF
Online Machine Learning: introduction and examples
PDF
Ranking and Diversity in Recommendations - RecSys Stammtisch at SoundCloud, B...
PDF
Data structures and algorithm analysis in java
PDF
Data exploration validation and sanitization
PPT
CPS(M): Constraint Satisfaction Problem over Models (a.k.a rule based design ...
PDF
Kaggle Days Madrid - Alberto Danese
PDF
[系列活動] 機器學習速遊
PDF
Mapping Domain Names to Categories
Recommendation algorithm using reinforcement learning
Introduction to Exploratory Data Analysis with the sci-analysis Python Package
Test for AI model
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
林守德/Practical Issues in Machine Learning
[系列活動] Machine Learning 機器學習課程
Tracking the tracker: Time Series Analysis in Python from First Principles
8. Graph - Data Structures using C++ by Varsha Patil
Kaggle Days Paris - Alberto Danese - ML Interpretability
[SIGIR17] Learning to Rank Using Localized Geometric Mean Metrics
Machine Learning and Model-Based Optimization for Heterogeneous Catalyst Desi...
Data structures algorithms_tutorial
Online Machine Learning: introduction and examples
Ranking and Diversity in Recommendations - RecSys Stammtisch at SoundCloud, B...
Data structures and algorithm analysis in java
Data exploration validation and sanitization
CPS(M): Constraint Satisfaction Problem over Models (a.k.a rule based design ...
Kaggle Days Madrid - Alberto Danese
[系列活動] 機器學習速遊
Mapping Domain Names to Categories
Ad

Similar to Data Structures and Algorithm - Week 5 - AVL Trees (20)

PPTX
Smart.ppt
PPTX
Avl trees
PPTX
Adelson velskii Landis rotations based on
PPT
lec41.ppt
PPTX
Lecture3
PPT
AVL Tree.ppt
PPTX
Data structures trees and graphs - AVL tree.pptx
PDF
data structure AVL TREES chapter slides for learning about AVL trees
PPTX
TREES_FINAL in data structure binary search tree.pptx
PPT
Presentation1 data structure for CSE.ppt
PPTX
AVL Tree Data Structure
PPT
Advanced Data Structures & Algorithm Analysi
PPTX
Lecture 10 data structures and algorithms
PDF
avl insertion-rotation
PPTX
Avl tree
PDF
Sienna6bst 120411102353-phpapp02
PPTX
AVL Tree.pptx
PDF
AVL Tree lecture slide with all rotations
PPTX
Unit3_3_btreekkkkkkkkkkkkkkkkkkkkkkk.pptx
Smart.ppt
Avl trees
Adelson velskii Landis rotations based on
lec41.ppt
Lecture3
AVL Tree.ppt
Data structures trees and graphs - AVL tree.pptx
data structure AVL TREES chapter slides for learning about AVL trees
TREES_FINAL in data structure binary search tree.pptx
Presentation1 data structure for CSE.ppt
AVL Tree Data Structure
Advanced Data Structures & Algorithm Analysi
Lecture 10 data structures and algorithms
avl insertion-rotation
Avl tree
Sienna6bst 120411102353-phpapp02
AVL Tree.pptx
AVL Tree lecture slide with all rotations
Unit3_3_btreekkkkkkkkkkkkkkkkkkkkkkk.pptx
Ad

More from Ferdin Joe John Joseph PhD (20)

PDF
Invited Talk DGTiCon 2022
PDF
Week 12: Cloud AI- DSA 441 Cloud Computing
PDF
Week 11: Cloud Native- DSA 441 Cloud Computing
PDF
Week 10: Cloud Security- DSA 441 Cloud Computing
PDF
Week 9: Relational Database Service Alibaba Cloud- DSA 441 Cloud Computing
PDF
Week 7: Object Storage Service Alibaba Cloud- DSA 441 Cloud Computing
PDF
Week 6: Server Load Balancer and Auto Scaling Alibaba Cloud- DSA 441 Cloud Co...
PDF
Week 5: Elastic Compute Service (ECS) with Alibaba Cloud- DSA 441 Cloud Compu...
PDF
Week 4: Big Data and Hadoop in Alibaba Cloud - DSA 441 Cloud Computing
PDF
Week 3: Virtual Private Cloud, On Premise, IaaS, PaaS, SaaS - DSA 441 Cloud C...
PDF
Week 2: Virtualization and VM Ware - DSA 441 Cloud Computing
PDF
Week 1: Introduction to Cloud Computing - DSA 441 Cloud Computing
PDF
Sept 6 2021 BTech Artificial Intelligence and Data Science curriculum
PDF
Hadoop in Alibaba Cloud
PDF
Cloud Computing Essentials in Alibaba Cloud
PDF
Transforming deep into transformers – a computer vision approach
PDF
Week 11: Programming for Data Analysis
PDF
Week 10: Programming for Data Analysis
PDF
Week 9: Programming for Data Analysis
PDF
Week 8: Programming for Data Analysis
Invited Talk DGTiCon 2022
Week 12: Cloud AI- DSA 441 Cloud Computing
Week 11: Cloud Native- DSA 441 Cloud Computing
Week 10: Cloud Security- DSA 441 Cloud Computing
Week 9: Relational Database Service Alibaba Cloud- DSA 441 Cloud Computing
Week 7: Object Storage Service Alibaba Cloud- DSA 441 Cloud Computing
Week 6: Server Load Balancer and Auto Scaling Alibaba Cloud- DSA 441 Cloud Co...
Week 5: Elastic Compute Service (ECS) with Alibaba Cloud- DSA 441 Cloud Compu...
Week 4: Big Data and Hadoop in Alibaba Cloud - DSA 441 Cloud Computing
Week 3: Virtual Private Cloud, On Premise, IaaS, PaaS, SaaS - DSA 441 Cloud C...
Week 2: Virtualization and VM Ware - DSA 441 Cloud Computing
Week 1: Introduction to Cloud Computing - DSA 441 Cloud Computing
Sept 6 2021 BTech Artificial Intelligence and Data Science curriculum
Hadoop in Alibaba Cloud
Cloud Computing Essentials in Alibaba Cloud
Transforming deep into transformers – a computer vision approach
Week 11: Programming for Data Analysis
Week 10: Programming for Data Analysis
Week 9: Programming for Data Analysis
Week 8: Programming for Data Analysis

Recently uploaded (20)

PDF
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
PDF
[EN] Industrial Machine Downtime Prediction
PPTX
Market Analysis -202507- Wind-Solar+Hybrid+Street+Lights+for+the+North+Amer...
PDF
Introduction to Data Science and Data Analysis
PPTX
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
PPTX
ALIMENTARY AND BILIARY CONDITIONS 3-1.pptx
PPTX
Introduction to Knowledge Engineering Part 1
PPT
Reliability_Chapter_ presentation 1221.5784
PPTX
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
PDF
Galatica Smart Energy Infrastructure Startup Pitch Deck
PPTX
1_Introduction to advance data techniques.pptx
PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
PDF
.pdf is not working space design for the following data for the following dat...
PPTX
Computer network topology notes for revision
PPTX
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
PPTX
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
PDF
Clinical guidelines as a resource for EBP(1).pdf
PPTX
STERILIZATION AND DISINFECTION-1.ppthhhbx
PDF
Introduction to the R Programming Language
PPTX
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
[EN] Industrial Machine Downtime Prediction
Market Analysis -202507- Wind-Solar+Hybrid+Street+Lights+for+the+North+Amer...
Introduction to Data Science and Data Analysis
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
ALIMENTARY AND BILIARY CONDITIONS 3-1.pptx
Introduction to Knowledge Engineering Part 1
Reliability_Chapter_ presentation 1221.5784
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
Galatica Smart Energy Infrastructure Startup Pitch Deck
1_Introduction to advance data techniques.pptx
Acceptance and paychological effects of mandatory extra coach I classes.pptx
.pdf is not working space design for the following data for the following dat...
Computer network topology notes for revision
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
Clinical guidelines as a resource for EBP(1).pdf
STERILIZATION AND DISINFECTION-1.ppthhhbx
Introduction to the R Programming Language
The THESIS FINAL-DEFENSE-PRESENTATION.pptx

Data Structures and Algorithm - Week 5 - AVL Trees

  • 1. Data Structures and Algorithms Week 5: AVL Trees Ferdin Joe John Joseph, PhD Faculty of Information Technology Thai-Nichi Institute of Technology, Bangkok
  • 2. Week 5 • AVL Trees • Implementation in Java Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 2
  • 3. AVL Trees • Named after Adelson, Velski and EM Landis Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 3
  • 4. Balance Balance == height(left subtree) - height(right subtree) • zero everywhere ⇒ perfectly balanced • small everywhere ⇒ balanced enough Balance between -1 and 1 everywhere ⇒ maximum height of ~1.44 log n t 5 7 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 4
  • 5. AVL Tree Dictionary Data Structure 4 121062 115 8 14137 9 Binary search tree properties • binary tree property • search tree property Balance property • balance of every node is: -1 ≤ b ≤ 1 15 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 5
  • 6. An AVL Tree 15 92 12 5 10 20 17 0 0 100 1 2 3 10 3 data height children 30 0 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 6
  • 7. Not AVL Trees 15 12 5 10 20 17 0 10 0 2 3 30 0 15 10 20 0 1 2 (-1)-1 = -2 0-2 = -2 Note: height(empty tree) == -1 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 7
  • 8. Good Insert Case: Balance Preserved Good case: insert middle, then small,then tall Insert(middle) Insert(small) Insert(tall) M S T 00 1 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 8
  • 9. Bad Insert Case #1: Left-Left or Right-Right Imbalance Insert(small) Insert(middle) Insert(tall) T M S 0 1 2 BC#1 Imbalance caused by either:  Insert into left child’s left subtree  Insert into right child’s right subtree Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 9
  • 10. Single Rotation T M S 0 1 2 M S T 00 1 Basic operation used in AVL trees: A right child could legally have its parent as its left child. Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 10
  • 11. General Bad Case #1 a X Y b Z h h - 1 h + 1 h - 1 h + 2 a X Y b Z h-1 h - 1 h h - 1 h + 1 Note: imbalance is left-left Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 11
  • 12. Single Rotation Fixes Case #1 Imbalance • Height of left subtree same as it was before insert! • Height of all ancestors unchanged • We can stop here! a X Y b Z a XY b Z h h - 1 h + 1 h - 1 h + 2 h h - 1 h h - 1 h + 1 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 12
  • 13. Bad Insert Case #2: Left-Right or Right-Left Imbalance Insert(small) Insert(tall) Insert(middle) M T S 0 1 2 Will a single rotation fix this? BC#2 Imbalance caused by either:  Insert into left child’s right subtree  Insert into right child’s left subtree Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 13
  • 14. Double Rotation M S T 00 1 M T S 0 1 2 T M S 0 1 2 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 14
  • 15. General Bad Case #2 a X b Z h h + 1 h - 1 h + 2 a X b Z h-1 h - 1 h h - 1 h + 1 Note: imbalance is left-right h-1 Y Y Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 15
  • 16. Double Rotation Fixes Case #2 Imbalance Initially: insert into either X or Y unbalances tree (root balance goes to 2 or -2) “Zig zag” to pull up c – restores root height to h+1, left subtree height to h a Z b W c X Y a Z b W c X Y h h - 1? h - 1 h - 1 h + 2 h + 1 h - 1h - 1 h h + 1 h h - 1? Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 16
  • 17. AVL Insert Algorithm • Find spot for value • Hang new node • Search back up looking for imbalance • If there is an imbalance: case #1: Perform single rotation case #2: Perform double rotation • Done! (There can only be one imbalance!) Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 17
  • 18. Easy Insert 2092 155 10 3017 Insert(3) 12 0 0 100 1 2 3 0 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 18
  • 19. Hard Insert (Bad Case #1) 2092 155 10 3017 Insert(33) 3 12 1 0 100 2 2 3 00 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 19
  • 20. Single Rotation 2092 155 10 30173 12 33 1 0 200 2 3 3 10 0 3092 205 10 333 15 1 0 110 2 2 3 00 1712 0 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 20
  • 21. Hard Insert (Bad Case #2) Insert(18) 2092 155 10 30173 12 1 0 100 2 2 3 00 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 21
  • 22. Single Rotation (oops!) 2092 155 10 30173 12 1 1 200 2 3 3 00 3092 205 10 3 15 1 1 020 2 3 3 0 1712 0 18 0 18 0 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 22
  • 23. Double Rotation (Step #1) 2092 155 10 30173 12 1 1 200 2 3 3 00 18 0 1792 155 10 203 12 1 200 2 3 3 10 30 0 Look familiar? 18 0 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 23
  • 24. Double Rotation (Step #2) 1792 155 10 203 12 1 200 2 3 3 10 30 0 18 0 2092 175 10 303 15 1 0 110 2 2 3 00 12 0 18 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 24
  • 25. AVL Insert Algorithm Revisited • Recursive 1. Search downward for spot 2. Insert node 3. Unwind stack, correcting heights a. If imbalance #1, single rotate b. If imbalance #2, double rotate • Iterative 1. Search downward for spot, stacking parent nodes 2. Insert node 3. Unwind stack, correcting heights a. If imbalance #1, single rotate and exit b. If imbalance #2, double rotate and exit Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 25
  • 26. Single Rotation Code void RotateRight(Node *& root) { Node * temp = root->right; root->right = temp->left; temp->left = root; root->height = max(root->right->height, root->left->height) + 1; temp->height = max(temp->right->height, temp->left->height) + 1; root = temp; } X Y Z root temp Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 26
  • 27. Double Rotation Code void DoubleRotateRight(Node *& root) { RotateLeft(root->right); RotateRight(root); } a Z b W c XY a Z c b X Y W First Rotation Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 27
  • 28. Double Rotation Completed a Z c b X Y W a Z c b XY W First Rotation Second Rotation Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 28
  • 29. Deletion: Really Easy Case 2092 155 10 30173 12 1 0 100 2 2 3 00 Delete(17) Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 29
  • 30. Deletion: Pretty Easy Case 2092 155 10 30173 12 1 0 100 2 2 3 00 Delete(15) Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 30
  • 31. Deletion: Pretty Easy Case (cont.) 2092 175 10 303 12 1 100 2 2 3 00 Delete(15) Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 31
  • 32. Deletion (Hard Case #1) 2092 175 10 303 12 1 100 2 2 3 00 Delete(12) Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 32
  • 33. Single Rotation on Deletion 2092 175 10 303 1 10 2 2 3 00 3092 205 10 17 3 1 00 2 1 3 0 0 Deletion can differ from insertion – How? Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 33
  • 34. Deletion (Hard Case) Delete(9) 2092 175 10 303 12 1 220 2 3 4 0 33 15 13 0 0 1 0 20 30 12 33 15 13 1 0 0 11 0 18 0 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 34
  • 35. Double Rotation on Deletion 2 3 0 202 175 10 30 12 1 22 2 3 4 33 15 13 1 0 0 1 11 0 18 0 2052 173 10 30 12 0 220 1 3 4 33 15 13 1 0 0 1 11 0 18 00 Not finished! Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 35
  • 36. Deletion with Propagation We get to choose whether to single or double rotate! 2052 173 10 30 12 0 220 1 3 4 33 15 13 1 0 0 1 11 0 18 0 What different about this case? Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 36
  • 37. Propagated Single Rotation 0 30 20 17 33 12 15 13 1 0 52 3 10 4 3 2 1 2 1 0 0 0 11 0 2052 173 10 30 12 0 220 1 3 4 33 15 13 1 0 1 11 0 18 0 18 0 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 37
  • 38. Propagated Double Rotation 0 17 12 11 52 3 10 4 2 3 1 0 0 0 2052 173 10 30 12 0 220 1 3 4 33 15 13 1 0 1 11 0 18 0 15 1 0 20 30 33 1 18 0 13 0 2 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 38
  • 39. AVL Deletion Algorithm • Recursive 1. Search downward for node 2. Delete node 3. Unwind, correcting heights as we go a. If imbalance #1, single rotate b. If imbalance #2 (or don’t care), double rotate • Iterative 1. Search downward for node, stacking parent nodes 2. Delete node 3. Unwind stack, correcting heights a. If imbalance #1, single rotate b. If imbalance #2 (or don’t care) double rotate Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 39
  • 40. Assignment • Write Java code to perform AVL tree based on the Fibonacci series of length and elements as user input. Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 40
  • 41. Next Week Red Black Trees Graph Theory Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 41