Title: AVL Trees
1Lecture 12
Prof. Sin-Min Lee Department of Computer Science
2Types of Trees
trees
dynamic
static
game trees
search trees
priority queues and heaps
graphs
Huffman coding tree
3Complete Binary Tree
- Is a tree with height h where
- Every node is full upto level h-2,
- Level h-1 is completely filled.
- Level h is filled from left to right.
- Yields to array representation. How to compute
indices of parent and child? - How many internal nodes and leafs?
4AVL Trees
- Balanced binary search tree offer a O(log n)
insert and delete. - But balancing itself costs O(n) in the average
case. - In this case, even though delete will be O(log
n), insert will be O(n). - Is there any way to have a O(log n) insert too?
- Yes, by almost but not fully balancing the tree
AVL (Adelson Velskii and Landis) balancing
5Height of a Tree
- Definition is same as level. Height of a tree is
the length of the longest path from root to some
leaf node. - Height of a empty tree is -1.
- Height of a single node tree is 0.
- Recursive definition
- height(t) 0 if number of nodes 1
- -1 if T is empty
- 1 max(height(LT), height(RT))
otherwise
6AVL Property
- If N is a node in a binary tree, node N has AVL
property if the heights of the left sub-tree and
right sub-tree are equal or if they differ by 1. - Lets look at some examples.
7AVL Tree Example
8Non-AVL Tree
9Transforming into AVL Tree
- Four different transformations are available
called rotations - Rotations single right, single left, double
right, double left - There is a close relationship between rotations
and associative law of algebra.
10Transformations
- Single right
- ((T1 T2) T3) (T1 (T2 T3)
- Single left
- (T1 (T2 T3)) ((T1 T2) T3)
- Double right
- ((T1 (T2 T3)) T4) ((T1T2) (T3T4))
- Double left
- (T1 ((T2T3) T4)) ((T1T2) (T3T4))
11Example AVL Tree for Airports
- Consider inserting sequentially ORY, JFK, BRU,
DUS, ZRX, MEX, ORD, NRT, ARN, GLA, GCM - Build a binary-search tree
- Build a AVL tree.
12Binary Search Tree for Airport Names
ORY
ZRH
JFK
MEX
BRU
ORD
DUS
ARN
NRT
GLA
GCM
13AVL Balancing Four Rotations
X1
X3
Double right
X2
X2
X1
Single right
X3
X1
X1
X3
X2
Single left
Double left
X2
X1
X3
X1
X2
X3
14An AVL Tree for Airport Names
After insertion of ORY, JFK and BRU
Single right
Not AVL balanced
AVL Balanced
15An AVL Tree for Airport Names (contd.)
After insertion of DUS, ZRH, MEX and ORD
After insertion of NRT?
DUS
MEX
ZRH
ORD
Still AVL Balanced
16An AVL Tree
Not AVL Balanaced
Now add ARN and GLA no need for rotations Then
add GCM
17An AVL Tree
JFK
GCM
ARN
DUS
GLA
Double left
NOT AVL BALANCED
18Search Operation
- For successful search, average number of
comparisons - sum of all (path length1) / number of nodes
- For the binary search tree (of airports) it is
- 39/11 3.55
- For the AVL tree (of airports) it is
- 33/11 3.0
19Known Performance Results of AVL trees
- AVL tree is a sub optimal solution.
- How to evaluate its performance?
- Bounds (upper bound and lower bound) for number
of comparisons - C gt log(n1) 1
- C lt 1.44 log(n2) - 0.328
- AVL trees are no more than 44 worse than optimal
trees.
20(No Transcript)
21(No Transcript)
22(No Transcript)
23Proposed by Adelson-Vels Skii G.M. and Landis
Y.M. (1962)
The definition of an AVL tree indicates that the
minimum number of nodes in a tree is determined
by the recurrence equation AVLh
AVLh-1 AVLh-2 1 where AVL0 0 and AVL1 1.
Example If the height is 1, there must be at
least 2 nodes in the AVL
tree.
24- BALANCE FACTORS
- Indicated by the numbers in the nodes
- Equal the height of right subtree minus the
height of left subtree - The height of empty node in AVL is -1 otherwise
it is 0 - All numbers should be 1, 0, or -1
EXAMPLES OF AVL TREES
1
1
-1
-1
-1
0
-1
0
0
0
1
0
0
25(No Transcript)
26(No Transcript)
27(No Transcript)
281) FIRST CASE -Resulted from inserting a
node in the right subtree of the right child.
Q
0
P
2
P
1
Q
P
1
1
0
Q
0
1
New node inserted
AVL tree is unbalanced.
The AVL tree is now balanced
29(No Transcript)
30(No Transcript)
31(No Transcript)
32(No Transcript)
332) SECOND CASE -Resulted from inserting a
node into the left subtree of the right
child. (it is more complex.)
P
P
2
1
1
P
Q
-1
-1
Q
Q
0
A
1
-1
R
A
R
R
R
P
0
2
Q
P
R
1
0
2
Q
0
A
34FACTS ABOUT NODE DELETION AND INSERTION
- Require at most 1.44lg(n2) searches.
- Require 1 single or 1 double rotation, depend
the balance factors - Deletion can require 1.44lg(n2) rotations in
the worst case. - May or may not need rotation at all
35 If append a node to a path of all zero
balance factor in AVL tree , no rotations
required except updating the the balance
factors of nodes.
0
-1
0
1
0
-1
0
1
Before updating the nodes
After updating the nodes
36 Example of insertion (with rotation) 20 40 60 .
20
40
20
40
40
60
20
60
37Example of insertion (with rotation) 20 40 60 10
15
40
40
20
20
20
60
40
60
40
20
10
60
15
40
15
60
10
20
20
38 Example of insertion (with rotation) 20 40 60 10
15 12 .
40
40
20
20
20
60
40
60
40
20
10
60
15
40
40
15
40
15
60
15
60
10
10
20
10
20
12
20
60
12
15
15
10
40
11
40
12
20
60
10
12
20
60
11
39(No Transcript)
40(No Transcript)
41 Deletion of a node may or may not requires
rotation .
B
0
2
A
1
C
B
A
1
1
0
0
C
Rotation required.
0
0
1
No rotation required
42FACTS ABOUT NODE DELETION AND INSERTION (cont.)
- 22 cases of deletion require rebalancing
- 47 cases of insertion require rebalancing
- Because the more time-consuming deletion
- occurs less frequently than insertion, it will
- not endangering the efficiency of rebalancing
- AVL trees.
43(No Transcript)
44(No Transcript)