What do Fibonacci numbers have to do with AVL trees??? Read on, fair reader, read on.
Recall that an AVL tree is a Binary Search Tree that maintains the invariant that the depths of its left and right subtrees differ by at most 1. In this note, we are going to explore how far an AVL tree can be from a complete binary tree. To make this more precise, we are going to answer the following two questions.
For a fixed depth what is the minimum number of nodes an AVL tree of depth must have? (i.e. how small can the tree be?)
For a fixed number of nodes what is maximum possible depth of an AVL tree with nodes?
We will first tackle (1).
ANSWER BELOW: The answer is .
In an AVL tree, each vertex has the property that the depths of its left and right subtrees differ by at most 1. In particular this is true of the root. Let us consider the subtrees of the root. One of them must have depth , since the depth of the whole tree is . The AVL property allows the other tree to have depth or (it can't be since that is the depth of the entire tree). The minimality of the size of the tree implies that in fact the depth of the second subtree must be . Furthermore, Since the entire tree is of minimal size, the subtrees themselves must be of minimal size for their depths, so that their sizes are and respectively. Adding 1 for the root gives the relevant recurrence.
In order to know the size of the minimal AVL tree of depth , we must solve this recurrence.
Look at that again. Does it look familiar? <Drumroll> Enter the Fibonacci numbers, stage left.
The Fibonacci numbers satisfy the recurrence
so our recurrence looks just like them, except that there is an extra 1 on the right hand side.
Now, for , let Then so the sequence really is the Fibonacci numbers!
But wait a minute, I hear you cry. What about the initial conditions? What about the base cases???
OK. Fair point. So lets examine them.
is only defined from 2 on, and and It follows that for all .
where the last equation is Binet's formula, a well-known fact about Fibonacci numbers.
In other words, where . This answers question (1)
On the other hand, the smallest complete binary tree of depth has size . (Why?) This means that an AVL tree of depth could be exponentially smaller than a complete binary tree of the same depth!
Complete trees which would be the ideal data structure for binary search, if we could build them efficiently. Given that AVL trees are so far from complete trees, why are AVL trees a good data structure for binary search?
To answer that, note that the controlling parameter for the time complexity of searching in a search tree is the depth of a tree of fixed size, not the size of a tree of fixed depth. In other words, we need to answer question (2).
Inverting the answer to question (1), we see that the answer to question (2) is . But this is only a constant factor bigger than , which is the depth of a complete binary tree with vertices. Thus searching in an AVL tree only gives up a constant factor time over searching in a complete binary tree, while making the other operations involved in maintaining the tree (insert, delete) significantly less time consuming.
Easy Math Editor
This discussion board is a place to discuss our Daily Challenges and the math and science related to those challenges. Explanations are more than just a solution — they should explain the steps and thinking strategies that you used to obtain the solution. Comments should further the discussion of math and science.
When posting on Brilliant:
*italics*
or_italics_
**bold**
or__bold__
paragraph 1
paragraph 2
[example link](https://brilliant.org)
> This is a quote
\(
...\)
or\[
...\]
to ensure proper formatting.2 \times 3
2^{34}
a_{i-1}
\frac{2}{3}
\sqrt{2}
\sum_{i=1}^3
\sin \theta
\boxed{123}
Comments
Okay, I am sorry if this bothers you, but can I ask how do you get the problems to have the looks like the one in the wikis. Is this limited to staff and selected members or not?
Log in to reply
I opened up a wiki page, clicked "edit wiki" and looked at how it was formatted there and copied that :D
And no, it doesn't bother me at all.
Log in to reply
Ohhh, clever! Thanks for telling me that!
Log in to reply
Log in to reply
There are many more mistakes in this that I can't say right now. It is best if you check a set with a similar format from a different creator to really see all of the mistakes if you and others used that format.
Here's one: Olay, let's try this. Brilliant needs to fix all of the problems they got with using similar concepts in a different writing environment, in this case, it's between the wikis and the notes.