1 lecture 12 avl trees. 2 trees static dynamic game treessearch trees priority queues and heaps...
TRANSCRIPT
2
trees
static dynamic
game trees search trees priority queues and heaps
graphs
binary searchtrees
AVL trees2-3 trees tries Huffman
coding tree
Types of TreesTypes of Trees
3
Complete Binary TreeComplete Binary Tree
Is a tree with height h where:Is a tree with height h where: Every node is full upto level h-2,Every node is full upto level h-2, Level h-1 is completely filled.Level h-1 is completely filled. Level h is filled from left to right.Level h is filled from left to right.
Yields to array representation. How to Yields to array representation. How to compute indices of parent and child?compute indices of parent and child?
How many internal nodes and leafs?How many internal nodes and leafs?
4
AVL TreesAVL Trees Balanced binary search tree offer a O(log n) Balanced binary search tree offer a O(log n)
insert and delete.insert and delete. But balancing itself costs O(n) in the But balancing itself costs O(n) in the
average case.average case. In this case, even though delete will be In this case, even though delete will be
O(log n), insert will be O(n).O(log n), insert will be O(n). Is there any way to have a O(log n) insert Is there any way to have a O(log n) insert
too?too? Yes, by almost but not fully balancing the Yes, by almost but not fully balancing the
tree : AVL (Adelson Velskii and Landis) tree : AVL (Adelson Velskii and Landis) balancingbalancing
5
Height of a TreeHeight of a Tree Definition is same as level. Height of a tree is Definition is same as level. Height of a tree is
the length of the longest path from root to the length of the longest path from root to some leaf node. some leaf node.
Height of a empty tree is -1.Height of a empty tree is -1. Height of a single node tree is 0.Height of a single node tree is 0. Recursive definition: Recursive definition: height(t) = 0 if number of nodes = 1height(t) = 0 if number of nodes = 1
= -1 if T is empty= -1 if T is empty = 1+ max(height(LT), height(RT)) = 1+ max(height(LT), height(RT))
otherwiseotherwise
6
AVL PropertyAVL Property
If N is a node in a binary tree, node If N is a node in a binary tree, node N has AVL property if the heights N has AVL property if the heights of the left sub-tree and right sub-of the left sub-tree and right sub-tree are equal or if they differ by 1.tree are equal or if they differ by 1.
Lets look at some examples.Lets look at some examples.
9
Transforming into AVL Transforming into AVL TreeTree
Four different transformations are Four different transformations are available called : rotationsavailable called : rotations
Rotations: single right, single left, Rotations: single right, single left, double right, double leftdouble right, double left
There is a close relationship There is a close relationship between rotations and associative between rotations and associative law of algebra.law of algebra.
10
TransformationsTransformations
Single right : Single right : ((T1 + T2) + T3) = (T1 + (T2 + T3)((T1 + T2) + T3) = (T1 + (T2 + T3) Single left : Single left : (T1 + (T2 + T3)) = ((T1 + T2) + T3)(T1 + (T2 + T3)) = ((T1 + T2) + T3) Double right : Double right : ((T1 + (T2 + T3)) + T4) = ((T1+T2) + ((T1 + (T2 + T3)) + T4) = ((T1+T2) +
(T3+T4))(T3+T4)) Double left :Double left :(T1 ((T2+T3) +T4)) = ((T1+T2) + (T3+T4))(T1 ((T2+T3) +T4)) = ((T1+T2) + (T3+T4))
11
Example: AVL Tree for Example: AVL Tree for AirportsAirports
Consider inserting Consider inserting sequentiallysequentially: : ORY, JFK, BRU, DUS, ZRX, MEX, ORY, JFK, BRU, DUS, ZRX, MEX, ORD, NRT, ARN, GLA, GCMORD, NRT, ARN, GLA, GCM
Build a binary-search treeBuild a binary-search tree Build a AVL tree.Build a AVL tree.
12
Binary Search Tree for Binary Search Tree for Airport NamesAirport Names
ORY
ZRHJFK
BRU MEX
ARNDUS
GLA
ORD
NRT
GCM
13
AVL Balancing : Four AVL Balancing : Four RotationsRotations
Single rightX3
X2
X1
X2
X1 X3
X1
X2
X3
Single left
X2
X3 X1 X3
X2
X1
Double rightX3
X2 X1
X3
X1 X2X2
X3
X1
Double left
14
An AVL Tree for Airport An AVL Tree for Airport NamesNames
ORY
JFK
BRU
Not AVL balanced
Single right JFK
BRU ORY
AVL Balanced
After insertion of ORY, JFK and BRU :
15
An AVL Tree for Airport An AVL Tree for Airport Names (contd.)Names (contd.)
After insertion of DUS, ZRH, MEX and ORD
JFK
BRU ORY
DUS MEX ZRH
ORD
Still AVL Balanced
After insertion of NRT?
JFK
BRU ORY
DUS MEX ZRH
ORD
NRT
16
An AVL Tree … An AVL Tree …
JFK
BRU ORY
DUS MEX ZRH
ORD
NRT
Not AVL Balanaced
Double Left
JFK
BRU ORY
DUS NRT ZRH
ORDMEX
Now add ARN and GLA; noneed for rotations; Then add GCM
17
An AVL Tree…An AVL Tree…JFK
BRU
DUS
ORY
NRT ZRH
ORDMEX
ARN
GLA
GCM
NOT AVL BALANCED
JFK
BRU
GCM
ORY
NRT ZRH
ORDMEX
ARN
GLA
Double left
DUS
18
Search OperationSearch Operation
For successful search, average number of For successful search, average number of comparisons: comparisons:
sum of all (path length+1) / number of sum of all (path length+1) / number of nodesnodes
For the binary search tree (of airports) it For the binary search tree (of airports) it is:is:
39/11 = 3.5539/11 = 3.55 For the AVL tree (of airports) it is : For the AVL tree (of airports) it is :
33/11 = 3.033/11 = 3.0
19
Known Performance Known Performance Results of AVL treesResults of AVL trees
AVL tree is a sub optimal solution.AVL tree is a sub optimal solution. How to evaluate its performance?How to evaluate its performance? Bounds (upper bound and lower Bounds (upper bound and lower
bound) for number of comparisons:bound) for number of comparisons:C > log(n+1) + 1C > log(n+1) + 1C < 1.44 log(n+2) - 0.328C < 1.44 log(n+2) - 0.328 AVL trees are no more than 44% AVL trees are no more than 44%
worse than optimal trees.worse than optimal trees.
23
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 +1where AVL0 = 0 and AVL1 = 1.
Example: If the height is 1, there must be at least 2 nodes in the AVL tree.
Proposed by Adel’son-Vel’s Skii G.M. and Landis Y.M. (1962)
24
EXAMPLES OF AVL TREES:
-1
0
+1
0 -1
0
+1
-1
0
-1
0 +1
0
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
28
1) FIRST CASE:
-Resulted from inserting a node in the right subtree of the right child.
+1
0
+1
+2
0
+1 0 +1
New node inserted AVL tree is unbalanced.
P
Q
P
Q P
Q
The AVL tree is now balanced
33
2) SECOND CASE: -Resulted from inserting a node into the left subtree of the right child. (it is more complex.)
+1
0
+2
-1
-1
+1
-1
+1
+2
+2
0
0
+1 0
P
Q
P
Q
PQ
R R
R
RQ
Q
PP
RA
A
A
34
FACTS ABOUT NODEFACTS ABOUT NODE DELETION DELETION ANDAND INSERTION:INSERTION:
• • Require at most 1.44lg(n+2) Require at most 1.44lg(n+2)
searches.searches.
• • Require 1 single or 1 double rotation, Require 1 single or 1 double rotation,
depend the balance factorsdepend the balance factors
• • Deletion can require 1.44lg(n+2) Deletion can require 1.44lg(n+2)
rotations in the worst case.rotations in the worst case. May or may not need rotation at allMay 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
0
0
0
-1
+1
-1
+1
Before updating the nodes After updating the nodes
37
20 40
40
20
60 40
60
20
40 20
10
15
60
40
15
10 20
60
20
Example of insertion (with rotation): 20 40 60 10 15
38
Example of insertion (with rotation) 20 40 60 10 15 12 .
20 40
40
20
60 40
60
20
40 20
10
15
60
40
15
10 20
60 40
15 60
10 20
12
10 15
40
12 20 60
15 10 40
12
11
20 60
15 11 40
10 12 20 60
41
• Deletion of a node may or may not requires rotation .
+1
0
0
+1
+2
+1
0
0
+1
0
Rotation required.
No rotation required
A
B
C
C
B
A
42
FACTS ABOUT NODEFACTS ABOUT NODE DELETION DELETION ANDAND INSERTION: INSERTION: (cont.)(cont.)
• • 22% 22% cases of deletion require rebalancing cases of deletion require rebalancing
• • 47% cases of insertion require rebalancing47% cases of insertion require rebalancing
• • Because the more time-consuming deletion Because the more time-consuming deletion
occurs less frequently than insertion, it willoccurs less frequently than insertion, it will
not endangering the efficiency of rebalancingnot endangering the efficiency of rebalancing
AVL trees.AVL trees.