AVLtree PDF
AVLtree PDF
AVLtree PDF
Binary trees
A binary tree is either
The empty binary tree, for which Ill write
Or a point (called a node) connected to two smaller
binary trees (called its children)
The children must not share any nodes.
An empty
binary tree
Another
nonempty
A nonempty binary tree
binary tree
height is 3
size is 5
AVL trees
AVL trees are binary trees with the following restrictions.
The empty tree is an AVL tree
A nonempty binary tree is AVL if
the height difference of the children is at most 1,
and
both children are AVL trees
AVL
Not AVL Not AVL
The question
We wish to access large amounts of data quickly.
Remember amount of information is proportional to
size of tree
and access time is proportional to the height of the
tree.
So the question is how high can an AVL tree of a given
size be?
We start by asking a closely related question:
How small can an AVL tree of a given height be?
1 1
2 2
3
4
4 7
5 12
12
10
size 6
0
1 2 3 4 5
height
minsize(h) dots 1
5
h+2 1 line
Typeset October 31, 2005 8
Discrete Math. for Engineering, 2005. Application slides 5 c Theodore Norvell, Memorial University
So invert h+2 1
1
5
1
s = h+2 1
5
h+2
5 (s+ 1) =
log 5 (s + 1) = h + 2
log 5 (s + 1) 2 = h
log 2 log2(s + 1) + log 5 2 = h
so maxheight(s)
= 1.44 log2(s + 1) 0.3
For example
maxheight(106) = 29
maxheight(109) = 43
maxheight(1012) = 58
This means large amounts of data can be accessed in a
small amount of time, if we store the data in AVL trees.
Graphing maxheight
h3
0 2 4 6 8 10 12 14 16 18 20
s