AVL Tree
CSE 332 - Section 4
Reminder
CC00 Meet the Staff due Tomorrow Jan. 30
please come to our office hours! 😄
Announcements
AVL Trees
According to lecture:
An AVL Tree is a tree data structure with the following properties:
AVL Trees
valid AVL tree structures
Structural Property
Order Property
Notice that this is just a Binary Search Tree (BST) with 1 additional property.
Wait, why?
❓
Data Structure | Find | Insert | Delete |
AVL Tree | O(log n) | O(log n) | O(log n) |
Unbalanced BST | O(h), h = height | O(h) | O(h) |
Linked List | O(n) | O(n)* | O(n)* |
Unsorted Array | O(n) | O(n)*** | O(n) |
Sorted Array | O(log n)** | O(n) | O(n) |
*at the head is O(1), elsewhere O(n) **using binary search ***if we check for dupes
Problem 0
What are the constraints on the data types you can store in an AVL tree?
When is an AVL tree preferred over another dictionary implementation, such as a HashMap?
Problem 0
AVL Tree Rotations Microteach
To fix an imbalance in an AVL tree after an insert:
AVL Tree Rotations
Case | Insert Location | Tree Rotation(s) |
1 | Left subtree of Left child (W) | Single right rotation |
2 | Right subtree of Left child (X) | Double left-right rotation |
3 | Left subtree of Right child (Y) | Double right-left rotation |
4 | Right subtree of Right child (Z) | Single left rotation |
b
c
p
W
X
Y
Z
a
V
p is the lowest problem node. Insert location was in w.
References that change:
Right Rotation
b
c
p
W
X
Y
Z
b
c
p
W
X
Y
Z
a
V
a
V
p is the lowest problem node. Insert location was in z.
References that change:
b
c
p
W
X
Y
Z
a
V
Problem 1 - Left Rotation
Tip for ex4: remember to update height field in nodes
Case 1
3
8
6
2
5
1
Let’s insert 1.
6
b
c
p
W
X
Y
Z
2
1
1. Identify the problem node p
2
6
3
1
5
8
2. Identify the insert location
3. Execute the tree rotation
2
6
3
1
8
5
3.1. Identify bad edges
3.2. Remove bad edges
3.3. Replace edges
Case | Insert Location | Tree Rotation(s) |
1 | Left of Left (W) | Single right rotation |
2 | Right of Left (X) | Double left-right rotation |
3 | Left of Right (Y) | Double right-left rotation |
4 | Right of Right (Z) | Single left rotation |
Case 4
Let’s insert 11.
b
c
p
W
X
Y
Z
1. Identify the problem node p
2. Identify the insert location
3. Execute the tree rotation
3
8
6
7
9
11
6
9
11
6
9
8
3
11
7
6
9
8
3
7
11
3.1. Identify bad edges
3.2. Remove bad edges
3.3. Replace edges
Case | Insert Location | Tree Rotation(s) |
1 | Left of Left (W) | Single right rotation |
2 | Right of Left (X) | Double left-right rotation |
3 | Left of Right (Y) | Double right-left rotation |
4 | Right of Right (Z) | Single left rotation |
Case 2
Let’s insert either 4 or 6.
b
c
p
W
X
Y
Z
1. Identify the problem node p
2. Identify the insert location
3. Execute the first tree rotation
3
8
7
1
5
3.1. Identify bad edges
3.2. Remove bad edges
3.3. Replace edges
4
6
7
4
6
For Case 2, we do the first rotation on node b
5
1
8
5
3
6
4
7
3
6
1
4
8
5
7
Case | Insert Location | Tree Rotation(s) |
1 | Left of Left (W) | Single right rotation |
2 | Right of Left (X) | Double left-right rotation |
3 | Left of Right (Y) | Double right-left rotation |
4 | Right of Right (Z) | Single left rotation |
Case 2
This is now similar to Case 1!
b
c
p
W
X
Y
Z
4. Execute the second tree rotation
4.1. Identify bad edges
4.2. Remove bad edges
4.3. Replace edges
We do the second rotation on node p
3
6
1
4
8
5
7
3
5
1
6
7
8
4
3
7
5
1
4
6
8
Case | Insert Location | Tree Rotation(s) |
1 | Left of Left (W) | Single right rotation |
2 | Right of Left (X) | Double left-right rotation |
3 | Left of Right (Y) | Double right-left rotation |
4 | Right of Right (Z) | Single left rotation |
Case 3
Let’s insert either 6 or 8.
b
c
p
W
X
Y
Z
1. Identify the problem node p
2. Identify the insert location
3. Execute the first tree rotation
3
9
5
7
10
3.1. Identify bad edges
3.2. Remove bad edges
3.3. Replace edges
6
8
5
7
6
8
For Case 3, we do the first rotation on node c
3
7
5
6
9
8
10
3
7
5
6
9
8
10
Case | Insert Location | Tree Rotation(s) |
1 | Left of Left (W) | Single right rotation |
2 | Right of Left (X) | Double left-right rotation |
3 | Left of Right (Y) | Double right-left rotation |
4 | Right of Right (Z) | Single left rotation |
Case 3
This is now similar to Case 4!
b
c
p
W
X
Y
Z
4. Execute the second tree rotation
4.1. Identify bad edges
4.2. Remove bad edges
4.3. Replace edges
We do the second rotation on node p
3
7
5
6
9
8
10
5
7
3
6
9
8
10
5
9
7
3
6
8
10
Case | Insert Location | Tree Rotation(s) |
1 | Left of Left (W) | Single right rotation |
2 | Right of Left (X) | Double left-right rotation |
3 | Left of Right (Y) | Double right-left rotation |
4 | Right of Right (Z) | Single left rotation |
Problem 2
Insert 10, 4, 5, 8, 9, 6, 11, 3, 2, 1, 14 into an initially empty AVL Tree
Problem 2
Case | Insert Location | Tree Rotation(s) |
1 | Left of Left (W) | Single right rotation |
2 | Right of Left (X) | Double left-right rotation |
3 | Left of Right (Y) | Double right-left rotation |
4 | Right of Right (Z) | Single left rotation |
b
c
p
W
X
Y
Z
Insert 10, 4, 5, 8, 9, 6, 11, 3, 2, 1, 14 into an initially empty AVL Tree
Problem 2
10
Case | Insert Location | Tree Rotation(s) |
1 | Left of Left (W) | Single right rotation |
2 | Right of Left (X) | Double left-right rotation |
3 | Left of Right (Y) | Double right-left rotation |
4 | Right of Right (Z) | Single left rotation |
b
c
p
W
X
Y
Z
No imbalances
Insert 10, 4, 5, 8, 9, 6, 11, 3, 2, 1, 14 into an initially empty AVL Tree
Problem 2
4
10
b
c
p
W
X
Y
Z
No imbalances
Case | Insert Location | Tree Rotation(s) |
1 | Left of Left (W) | Single right rotation |
2 | Right of Left (X) | Double left-right rotation |
3 | Left of Right (Y) | Double right-left rotation |
4 | Right of Right (Z) | Single left rotation |
Insert 10, 4, 5, 8, 9, 6, 11, 3, 2, 1, 14 into an initially empty AVL Tree
5
4
10
b
c
p
W
X
Y
Imbalance at 10
Z
10
5
Case | Insert Location | Tree Rotation(s) |
1 | Left of Left (W) | Single right rotation |
2 | Right of Left (X) | Double left-right rotation |
3 | Left of Right (Y) | Double right-left rotation |
4 | Right of Right (Z) | Single left rotation |
Problem 2
4
5
10
b
c
p
W
X
Y
Imbalance at 10
Z
10
5
Insert 10, 4, 5, 8, 9, 6, 11, 3, 2, 1, 14 into an initially empty AVL Tree
Case | Insert Location | Tree Rotation(s) |
1 | Left of Left (W) | Single right rotation |
2 | Right of Left (X) | Double left-right rotation |
3 | Left of Right (Y) | Double right-left rotation |
4 | Right of Right (Z) | Single left rotation |
Problem 2
4
10
5
b
c
p
W
X
Y
Imbalance at 10
Z
No more imbalances
10
5
Insert 10, 4, 5, 8, 9, 6, 11, 3, 2, 1, 14 into an initially empty AVL Tree
Case | Insert Location | Tree Rotation(s) |
1 | Left of Left (W) | Single right rotation |
2 | Right of Left (X) | Double left-right rotation |
3 | Left of Right (Y) | Double right-left rotation |
4 | Right of Right (Z) | Single left rotation |
Problem 2
Insert 10, 4, 5, 8, 9, 6, 11, 3, 2, 1, 14 into an initially empty AVL Tree
4
10
5
8
b
c
p
W
X
Y
No imbalances
Z
Case | Insert Location | Tree Rotation(s) |
1 | Left of Left (W) | Single right rotation |
2 | Right of Left (X) | Double left-right rotation |
3 | Left of Right (Y) | Double right-left rotation |
4 | Right of Right (Z) | Single left rotation |
Problem 2
Insert 10, 4, 5, 8, 9, 6, 11, 3, 2, 1, 14 into an initially empty AVL Tree
4
10
5
9
8
b
c
p
W
X
Y
Imbalance at 10
Z
10
9
Case | Insert Location | Tree Rotation(s) |
1 | Left of Left (W) | Single right rotation |
2 | Right of Left (X) | Double left-right rotation |
3 | Left of Right (Y) | Double right-left rotation |
4 | Right of Right (Z) | Single left rotation |
Problem 2
4
10
5
8
9
b
c
p
W
X
Y
Z
10
9
Imbalance at 10
Insert 10, 4, 5, 8, 9, 6, 11, 3, 2, 1, 14 into an initially empty AVL Tree
Case | Insert Location | Tree Rotation(s) |
1 | Left of Left (W) | Single right rotation |
2 | Right of Left (X) | Double left-right rotation |
3 | Left of Right (Y) | Double right-left rotation |
4 | Right of Right (Z) | Single left rotation |
Problem 2
No more imbalances
4
9
5
8
10
b
c
p
W
X
Y
Z
10
9
Imbalance at 10
Insert 10, 4, 5, 8, 9, 6, 11, 3, 2, 1, 14 into an initially empty AVL Tree
Case | Insert Location | Tree Rotation(s) |
1 | Left of Left (W) | Single right rotation |
2 | Right of Left (X) | Double left-right rotation |
3 | Left of Right (Y) | Double right-left rotation |
4 | Right of Right (Z) | Single left rotation |
Problem 2
Insert 10, 4, 5, 8, 9, 6, 11, 3, 2, 1, 14 into an initially empty AVL Tree
4
9
5
6
8
10
b
c
p
W
X
Y
Imbalance at 5
Z
5
6
8
Case | Insert Location | Tree Rotation(s) |
1 | Left of Left (W) | Single right rotation |
2 | Right of Left (X) | Double left-right rotation |
3 | Left of Right (Y) | Double right-left rotation |
4 | Right of Right (Z) | Single left rotation |
Problem 2
4
8
5
6
9
10
b
c
p
W
X
Y
Z
5
8
Imbalance at 5
6
Insert 10, 4, 5, 8, 9, 6, 11, 3, 2, 1, 14 into an initially empty AVL Tree
Case | Insert Location | Tree Rotation(s) |
1 | Left of Left (W) | Single right rotation |
2 | Right of Left (X) | Double left-right rotation |
3 | Left of Right (Y) | Double right-left rotation |
4 | Right of Right (Z) | Single left rotation |
Problem 2
No more imbalances
4
6
5
9
8
10
b
c
p
W
X
Y
Z
5
8
Imbalance at 5
6
Insert 10, 4, 5, 8, 9, 6, 11, 3, 2, 1, 14 into an initially empty AVL Tree
Case | Insert Location | Tree Rotation(s) |
1 | Left of Left (W) | Single right rotation |
2 | Right of Left (X) | Double left-right rotation |
3 | Left of Right (Y) | Double right-left rotation |
4 | Right of Right (Z) | Single left rotation |
Problem 2
4
6
5
9
8
10
11
b
c
p
W
X
Y
Imbalance at 9
Z
9
11
Insert 10, 4, 5, 8, 9, 6, 11, 3, 2, 1, 14 into an initially empty AVL Tree
Case | Insert Location | Tree Rotation(s) |
1 | Left of Left (W) | Single right rotation |
2 | Right of Left (X) | Double left-right rotation |
3 | Left of Right (Y) | Double right-left rotation |
4 | Right of Right (Z) | Single left rotation |
Problem 2
4
6
5
10
8
9
11
b
c
p
W
X
Y
Z
No more imbalances
9
11
Imbalance at 9
Insert 10, 4, 5, 8, 9, 6, 11, 3, 2, 1, 14 into an initially empty AVL Tree
Case | Insert Location | Tree Rotation(s) |
1 | Left of Left (W) | Single right rotation |
2 | Right of Left (X) | Double left-right rotation |
3 | Left of Right (Y) | Double right-left rotation |
4 | Right of Right (Z) | Single left rotation |
Problem 2
Insert 10, 4, 5, 8, 9, 6, 11, 3, 2, 1, 14 into an initially empty AVL Tree
3
4
6
5
10
8
9
11
b
c
p
W
X
Y
No imbalances
Z
Case | Insert Location | Tree Rotation(s) |
1 | Left of Left (W) | Single right rotation |
2 | Right of Left (X) | Double left-right rotation |
3 | Left of Right (Y) | Double right-left rotation |
4 | Right of Right (Z) | Single left rotation |
Problem 2
Insert 10, 4, 5, 8, 9, 6, 11, 3, 2, 1, 14 into an initially empty AVL Tree
3
4
6
5
10
8
9
11
b
c
p
W
X
Y
Imbalance at 4
Z
4
2
2
Case | Insert Location | Tree Rotation(s) |
1 | Left of Left (W) | Single right rotation |
2 | Right of Left (X) | Double left-right rotation |
3 | Left of Right (Y) | Double right-left rotation |
4 | Right of Right (Z) | Single left rotation |
Problem 2
2
4
3
6
5
10
8
9
11
b
c
p
W
X
Y
Z
No more imbalances
4
2
Insert 10, 4, 5, 8, 9, 6, 11, 3, 2, 1, 14 into an initially empty AVL Tree
Imbalance at 4
Case | Insert Location | Tree Rotation(s) |
1 | Left of Left (W) | Single right rotation |
2 | Right of Left (X) | Double left-right rotation |
3 | Left of Right (Y) | Double right-left rotation |
4 | Right of Right (Z) | Single left rotation |
Problem 2
Insert 10, 4, 5, 8, 9, 6, 11, 3, 2, 1, 14 into an initially empty AVL Tree
2
4
3
6
5
10
8
9
11
b
c
p
W
X
Y
Imbalance at 5
Z
5
1
1
2
Case | Insert Location | Tree Rotation(s) |
1 | Left of Left (W) | Single right rotation |
2 | Right of Left (X) | Double left-right rotation |
3 | Left of Right (Y) | Double right-left rotation |
4 | Right of Right (Z) | Single left rotation |
Problem 2
1
2
5
3
10
8
4
6
9
11
b
c
p
W
X
Y
Z
No more imbalances
5
2
Imbalance at 5
1
Insert 10, 4, 5, 8, 9, 6, 11, 3, 2, 1, 14 into an initially empty AVL Tree
Case | Insert Location | Tree Rotation(s) |
1 | Left of Left (W) | Single right rotation |
2 | Right of Left (X) | Double left-right rotation |
3 | Left of Right (Y) | Double right-left rotation |
4 | Right of Right (Z) | Single left rotation |
Problem 2
Insert 10, 4, 5, 8, 9, 6, 11, 3, 2, 1, 14 into an initially empty AVL Tree
1
2
5
3
10
8
4
6
9
11
14
b
c
p
W
X
Y
No imbalances
Z
Case | Insert Location | Tree Rotation(s) |
1 | Left of Left (W) | Single right rotation |
2 | Right of Left (X) | Double left-right rotation |
3 | Left of Right (Y) | Double right-left rotation |
4 | Right of Right (Z) | Single left rotation |
Problem 2
Problem 3
Draw an AVL tree of height 4 that contains the minimum possible number of nodes.
Problem 3
Draw an AVL tree of height 4 that contains the minimum possible number of nodes.
Height = 0:
Problem 3
Draw an AVL tree of height 4 that contains the minimum possible number of nodes.
Height = 1:
Problem 3
Draw an AVL tree of height 4 that contains the minimum possible number of nodes.
Height = 2:
Problem 3
Draw an AVL tree of height 4 that contains the minimum possible number of nodes.
Height = 3:
Problem 3
Draw an AVL tree of height 4 that contains the minimum possible number of nodes.
Height = 4:
Problem 3
More generally: What is the min number of nodes for a AVL tree with height h?
height(r) = h
height(sub1) = h-1
height(sub2) = h-2
minNum(h) = 1 + minNum(h-1) + minNum(h-2)
r
sub1
sub2