Upload
colette-myers
View
62
Download
3
Embed Size (px)
DESCRIPTION
AVL Trees. B.Ramamurthy. Types of Trees. binary search trees. AVL trees. 2-3 trees. tries. trees. dynamic. static. game trees. search trees. priority queues and heaps. graphs. Huffman coding tree. Height of a Tree (Review). - PowerPoint PPT Presentation
Citation preview
04/19/23 BR 1
AVL Trees
B.Ramamurthy
04/19/23 BR 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 Trees
04/19/23 BR 3
Height of a Tree (Review)
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) = -1 if T is empty = 0 if number of nodes = 1
= 1+ max(height(LT), height(RT)) otherwise
04/19/23 BR 4
Balanced Binary Search Trees
Let n be the number of nodes (keys) in a binary search tree and h be its height.Binary search tree offers a O(h) insert and delete if the tree is balanced. h is (log n) for a balanced tree. Height-balanced(k) tree has for every
node left subtree and right subtree differ in height at most by k.
04/19/23 BR 5
Unbalanced Tree
65
54
45
78
87
98
Maximum height
04/19/23 BR 6
Balanced Tree
87
78
98
87
87 87
Minimum Height
04/19/23 BR 7
AVL Property
AVL (Adelson, Velskii and Landis) tree is a height-balanced(1) tree that follows the property stated below: For every internal node v of the tree, the
heights of the children of v differ by at most 1.
AVL property maintains a logarithmic height in terms of the number of nodes of the tree thus achieving O(log n) for insert and delete.Lets look at some examples.
04/19/23 BR 8
AVL Tree: Example
04/19/23 BR 9
Non-AVL Tree
04/19/23 BR 10
Transforming into AVL Tree
Four different transformations are available called : rotationsRotations: single right, single left, double right, double leftThere is a close relationship between rotations and associative law of algebra.
04/19/23 BR 11
Transformations
Single right : ((T1 T2) T3) = (T1 (T2 T3)
Single left : (T1 (T2 T3)) = ((T1 T2) T3)
Double right : ((T1 (T2 T3)) T4) = ((T1 T2) (T3 T4))
Double left :(T1 ((T2 T3) T4)) = ((T1 T2) (T3 T4))B
C
C
A B A B
A
A
A
BB
B B
A
AA C
B C
A,B,C are nodes, Tn (n =1,2,3,4..) are subtrees
04/19/23 BR 12
AVL Balancing : Four Rotations
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
04/19/23 BR 13
Example: AVL Tree for Airports
Consider inserting sequentially: ORY, JFK, BRU, DUS, ZRX, MEX, ORD, NRT, ARN, GLA, GCMBuild a binary-search treeBuild a AVL tree.
04/19/23 BR 14
Binary Search Tree for Airport Names
ORY
ZRHJFK
BRU MEX
ARNDUS
GLA
ORD
NRT
GCM
04/19/23 BR 15
An AVL Tree for Airport Names
ORY
JFK
BRU
Not AVL balanced
Single right JFK
BRU ORY
AVL Balanced
After insertion of ORY, JFK and BRU :
04/19/23 BR 16
An AVL Tree for Airport 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
04/19/23 BR 17
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
04/19/23 BR 18
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
04/19/23 BR 19
Search Operation
For successful search, average number of comparisons:
sum of all (path length to node+1) / number of nodesFor the binary search tree (of airports) it is:
39/11 = 3.55For the AVL tree (of airports) it is :
33/11 = 3.0