Application: AVL trees and the golden ratio AVL trees are used for storing information in an efficient manner. We will see exactly how in the data structures course. This slide set takes a look at how high an AVL tree of agivensizecanbe. The golden ratio Discrete Math. for Engineering,. Application slides Binary trees A binary tree is either The empty binary tree, for which I ll 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 The golden ratio is an irrational number φ = + =.68 with many interesting properties. Among them φ =/φ φ =+ + +... φ turns up in many geometric figures including pentagrams and dodecahedra It is the ratio, in the limit, of successive members of the Fibonacci sequence A nonempty binary tree Another nonempty binary tree Typeset October 3, Typeset October 3,
The height and size of a binary tree Discrete Math. for Engineering,. Application slides AVL trees The size of a binary tree is the number of nodes it has. The height of a binary tree is number of levels of nodes it has height is 3 AVL trees are binary trees with the following restrictions. TheemptytreeisanAVLtree A nonempty binary tree is AVL if the height difference of the children is at most, and both children are AVL trees size is Note that has height and size. Clearly a binary tree of size n canhaveaheightofupto n. AVL Not AVL Not AVL When binary trees are used to store data: The amount of information stored is proportional to size of tree The time to access data is proportional to the height Typeset October 3, 3 Typeset October 3,
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. SothequestionishowhighcananAVLtreeofagiven size be? We start by asking a closely related question: HowsmallcananAVLtreeofagivenheightbe? Discrete Math. for Engineering,. Application slides How small can an AVL tree of a given height be? Let smakeatablewiththesmallestavltreeofeach height (empty trees are implied) Height Smallest tree Size 3 7 Typeset October 3, Typeset October 3, 6
The minsize function In the table, each tree (of height h>) has, as children, smallest trees of heights h and h So we have minsize() = minsize() = minsize(h) = minsize(h ) + minsize(h ) +, forh Note the recurrence is not homogeneous. Try a few values,,,, 7,,, 33, Compare with the Fibonacci sequence,,, 3,, 8, 3,, 3, We find minsize(h) =fib(h +) where fib() = fib() = fib(n) =fib(n ) + fib(n ), forn We can prove this by (complete induction). Typeset October 3, 7 Discrete Math. for Engineering,. Application slides Since fib is defined by a linear homogeneous recurrence relationofdegreewecansolveit fib(n) = φ n+ ( for all n N where φ )n+ φ = + Consider φ n+ ( φ )n+ for n R and n. The first term is real, the second is complex. As n gets big, the complex term becomes small. So we get minsize(h) = φ h+ 8 size 6 minsize(h) dots 3 height φ h+ line Typeset October 3, 8
The maximum height per given size Discrete Math. for Engineering,. Application slides So invert φ h+ Height 3 Min size 7 Let h be the height of a tree of size s. We know that for all h, h h s minsize(h) Contrapositively: For all h, s < minsize(h) h <h Size 3 6 7 8 9 3 Max height 3 3 3 Note that for s such that minsize(h ) <s minsize(h) maxheight(s) =h maxheight(s) is approximately an inverse of minsize(h) s = φ h+ (s +)=φ h+ log φ (s +)=h + log φ (s +) =h log φ log (s +)+log φ =h so maxheight(s) =. log (s +).3 For example maxheight( 6 ) = 9 maxheight( 9 ) = 3 maxheight( ) = 8 This means large amounts of data can be accessed in a small amount of time, if we store the data in AVL trees. Typeset October 3, 9 Typeset October 3,
Graphing maxheight 6 h3 6 8 6 8 s maxheight(s) dots. log (s +).3 line Typeset October 3,