0% found this document useful (0 votes)
1 views43 pages

AVL Trees

The document provides an overview of AVL trees, a type of self-balancing binary search tree, detailing their properties, insertion and deletion algorithms, and how to maintain balance through rotations. It explains the conditions for imbalance and the necessary single or double rotations to restore balance after insertions or deletions. The document includes examples and code snippets for implementing AVL tree operations.

Uploaded by

asinha3be23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views43 pages

AVL Trees

The document provides an overview of AVL trees, a type of self-balancing binary search tree, detailing their properties, insertion and deletion algorithms, and how to maintain balance through rotations. It explains the conditions for imbalance and the necessary single or double rotations to restore balance after insertions or deletions. The document includes examples and code snippets for implementing AVL tree operations.

Uploaded by

asinha3be23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 43

UMT 404

AVL Trees

Dr. Anil Kumar Pal

Mechanical Engineering Department


TIET
Balance
Balance == height(left subtree) - height(right
subtree)
 zero everywhere  perfectly balanced
 small everywhere  balanced enough t

Balance between -1 and 1 everywhere


 maximum height ~1.44 log n
5
7

AVL Trees UMT 404 2


AVL Tree
Dictionary Data Structure
Binary search tree
properties 8
 binary tree property
 search tree property
5 11
Balance property
 balance of every node is:
-1 b  1 2 6 10 12
 result:
 depth is O(log n)
4 7 9 13 14

15
AVL Trees UMT 404 3
An AVL Tree
3 10 data
10 3 height
children
1 2
5 15
0 0 0 1
2 9 12 20
0 0
17 30

AVL Trees UMT 404 4


Not AVL Trees
3 0-2 = -2
10 2 (-1)-1 = -2
10
0 2
5 15 1
15
0 1
12 20 0
20
0 0
17 30

Note: height(empty tree) ==


-1

AVL Trees UMT 404 5


Good Insert Case:
Balance Preserved
Good case: insert middle, then small,then
tall

Insert(middle) M
Insert(small)
Insert(tall) 0 0
S T

AVL Trees UMT 404 6


Bad Insert Case #1:
Left-Left or Right-Right
Imbalance
Insert(small)
Insert(middle)
2
Insert(tall) S
1
BC#1 Imbalance caused by M
either:
 Insert into left child’s left subtree 0
 Insert into right child’s right T
subtree

AVL Trees UMT 404 7


Single Rotation
2
S 1
M
1
M
0 0
0 S T
T
Basic operation used in AVL trees:
A right child could legally have its parent as its left child.

AVL Trees UMT 404 8


General Bad Case #1
h+2
a h+1 a

h h-1 h+1 b h-1


b
X X

h-1 h-1 h h-1


Z Y Y

Note: imbalance is left-


left
AVL Trees UMT 404 9
Single Rotation
Fixes Case #1 Imbalance
a h+2 b h+1

h
h+1 b h-1 h a
X
h h-1 h-1
h-1 Z
Y Y X

Z
 Height of left subtree same as it was before insert!
 Height of all ancestors unchanged
 We can stop here!

AVL Trees UMT 404 10


Bad Insert Case #2:
Left-Right or Right-Left
Imbalance
Insert(small)
Insert(tall)
Insert(middle) 2
S
1
BC#2 Imbalance caused by
either: T
 Insert into left child’s right subtree
0
 Insert into right child’s left subtree M
Will a single rotation fix
this?
AVL Trees UMT 404 11
Double Rotation

2 2
S S 1
M
1 1
T M
0 0
0 S T
0
M T

AVL Trees UMT 404 12


General Bad Case #2
h+2
a h+1 a

h h-1 h+1 b h-1


b
X X

h-1 h-1
h-1 h
Z Y Z

Note: imbalance is left-


right
AVL Trees UMT 404 13
Double Rotation
Fixes Case #2 Imbalance
a h+2 h+1
h+1
h-1 c
b h h
h-1
b a
h Z
c h-1 h-1
X Y
W
X Y
W Z

h - 1?
h - 1?
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

AVL Trees UMT 404 14


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!)

AVL Trees UMT 404 15


Easy Insert
3
Insert(3) 10

1 2
5 15

0 0 0 1
2 9 12 20
0 0
17 30

AVL Trees UMT 404 16


Hard Insert (Bad Case #1)
3
Insert(33) 10

2 2
5 15
1 0 0 1
2 9 12 20
0 0 0
3 17 30

AVL Trees UMT 404 17


Single Rotation
3 3
10 10

2 3 2 2
5 15 5 20
1 0 0 2 1 0 1 1
2 9 12 20 2 9 15 30
0 0 1 0 0 0 0
3 17 30 3 12 17 33
0
33
AVL Trees UMT 404 18
Hard Insert (Bad Case #2)
3
Insert(18) 10

2 2
5 15
1 0 0 1
2 9 12 20
0 0 0
3 17 30

AVL Trees UMT 404 19


Single Rotation (oops!)
3 3
10 10

2 3 2 3
5 15 5 20
1 0 0 2 1 0 2 0
2 9 12 20 2 9 15 30
0 1 0 0 0 1
3 17 30 3 12 17
0 0
18 18
AVL Trees UMT 404 20
Double Rotation (Step #1)
3 3
10 10

2 3 2 3
5 15 5 15
1 0 0 2 1 0 0 2
2 9 12 20 2 9 12 17
0 1 0 0 1
3 17 30 3 20
0 0 0
18 Look familiar? 18 30
AVL Trees UMT 404 21
Double Rotation (Step #2)
3 3
10 10

2 3 2 2
5 15 5 17
1 0 0 2 1 0 1 1
2 9 12 17 2 9 15 20
0 1 0 0 0 0
3 20 3 12 18 30
0 0
18 30
AVL Trees UMT 404 22
AVL Insert Algorithm
Revisited
 Recursive  Iterative
1. Search downward for 1. Search downward for
spot, stacking
spot
parent nodes
2. Insert node 2. Insert node
3. Unwind stack, 3. Unwind stack,
correcting heights correcting heights
a. If imbalance #1, a. If imbalance #1,
single rotate single rotate and
exit
b. If imbalance #2,
b. If imbalance #2,
double rotate double rotate and
exit

AVL Trees UMT 404 23


Single Rotation Code
void RotateRight(Node *& root) {
Node * temp = root->right; root
root->right = temp->left;
temp->left = root; temp
root->height = X
max(root->right->height,
root->left->height) + 1; Y
temp->height =
max(temp->right->height,
Z
temp->left->height) + 1;
root = temp;
}

AVL Trees UMT 404 24


Double Rotation Code
void DoubleRotateRight(Node *& root) {
RotateLeft(root->right);
RotateRight(root);
}

a a First Rotation

b c

Z Z
c Y b
W
Y X X
W
AVL Trees UMT 404 25
Double Rotation
Completed
First Rotation Second Rotation
a c

c
a b
Z
Y b Y X

X Z W

AVL Trees UMT 404 26


Deletion: Really Easy Case
3
Delete(17) 10

2 2
5 15
1 0 0 1
2 9 12 20
0 0 0
3 17 30

AVL Trees UMT 404 27


Deletion: Pretty Easy Case
3
Delete(15) 10

2 2
5 15
1 0 0 1
2 9 12 20
0 0 0
3 17 30

AVL Trees UMT 404 28


Deletion: Pretty Easy Case
(cont.)
3
Delete(15) 10

2 2
5 17
1 0 0 1
2 9 12 20
0 0
3 30

AVL Trees UMT 404 29


Deletion (Hard Case #1)
Delete(12) 3
10

2 2
5 17
1 0 0 1
2 9 12 20
0 0
3 30

AVL Trees UMT 404 30


Single Rotation on
Deletion
3 3
10 10

2 2 2 1
5 17 5 20
1 0 1 1 0 0 0
2 9 20 2 9 17 30
0 0 0
3 30 3

Deletion can differ from insertion – How?

AVL Trees UMT 404 31


Deletion (Hard Case)
Delete(9) 4
10

2 3
5 17
1 0 2 2
2 9 12 20
0 0 1 0 1
3 11 15 18 30
0 0
13 33
AVL Trees UMT 404 32
Double Rotation on
Deletion
4 4 Not
10 10 finished!

2 3 1 3
5 17 3 17
1 2 2 0 0 2 2
2 12 20 2 5 12 20
0 0 1 0 1 0 1 0 1
3 11 15 18 30 11 15 18 30
0 0 0 0
13 33 13 33
AVL Trees UMT 404 33
Deletion with Propagation
4
10
What different about this case?
1 3
3 17
0 0 2 2
2 5 12 20
We get to choose whether
0 1 0 1 to single or double rotate!
11 15 18 30
0 0
13 33

AVL Trees UMT 404 34


Propagated Single
Rotation
4 4
10 17
1 3 3 2
3 17 10 20
0 0 2 2 1 2 0 1
2 5 12 20 3 12 18 30
0 1 0 1 0 0 0 1 0
11 15 18 30 2 5 11 15 33
0 0 0
13 33 13

AVL Trees UMT 404 35


Propagated Double
Rotation
4 4
10 12
1 3 2 3
3 17 10 17
0 0 2 2 1 0 1 2
2 5 12 20 3 11 15 20
0 1 0 1 0 0 0 0 1
11 15 18 30 2 5 13 18 30
0 0 0
13 33 33

AVL Trees UMT 404 36


AVL Deletion Algorithm
 Recursive  Iterative
1. Search downward for 1. Search downward for
node node, stacking
2. Delete node parent nodes
3. Unwind, correcting 2. Delete node
heights as we go 3. Unwind stack,
a. If imbalance #1, correcting heights
a. If imbalance #1,
single rotate
single rotate
b. If imbalance #2
(or don’t b. If imbalance #2
(or don’t care)
care),
double rotate
double rotate

AVL Trees UMT 404 37


Building an AVL Tree
Input: sequence of n keys (unordered)
19 3 4 18 7
Insert each into initially empty AVL tree
n n

 log i  log n O(n log n)


i 1 i 1

But, suppose input is already sorted …


3 4 7 18 19
Can we do better than O(n log n)?

AVL Trees UMT 404 38


AVL BuildTree
5 8 10 15 17 20 30 35 40

Divide & Conquer


 Divide the problem into
parts 17
 Solve each part
recursively
 Merge the parts into a
general solution
How long does
divide & conquer take? 5 8 1015 20303540

AVL Trees UMT 404 39


Build Tree Example
5 8 10 15 17 20 30 35 40

3
17
5 8 10 15 2 2 20 30 35 40
10 35
5 8 1 0 20 30 1 0
8 15 30 40
0 0
5 20

AVL Trees UMT 404 40


Build Tree Analysis
(Approximate)
T(1) = 1
T(n) = 2T(n/2) + 1
T(n) = 2(2T(n/4)+1) + 1
T(n) = 4T(n/4) + 2 + 1
T(n) = 4(2T(n/8)+1) + 2 + 1
T(n) = 8T(n/8) + 4 +k 2 + 1
1
T(n) = 2 T(n/2 ) + 2 i 1
k k  2 i

let 2k = n, log n log=n k


1
T(n) = nT(1) + 2  2
i

i 1
T(n) = (n)

AVL Trees UMT 404 41


Build Tree Analysis
(Exact)
Precise Analysis: T(0) = b
T(n) = T(  n 2 1) + T(  n 2)1 + c
By induction on n:
T(n) = (b+c)n + b
Base case:
T(0) = b = (b+c)0 + b
Induction step:
T(n) = (b+c)  n  1 + b +
 2 
(b+c)  n  1 + b + c
= (b+c)n 2+ b  n  1  n  1
QED: T(n) = (b+c)n + b = (n)  2    2  n  1

AVL Trees UMT 404 42


Thinking About AVL
Observations
+ Worst case height of an AVL tree is about 1.44
log n
+ Insert, Find, Delete in worst case O(log n)
+ Only one (single or double) rotation needed
on insertion
- O(log n) rotations needed on deletion

AVL Trees UMT 404 43

You might also like