What are the minimum number of nodes required for an AVL tree with height 7?

C++Server Side ProgrammingProgramming

Show

    Given the height of an AVL tree, the task is to find the minimum number of nodes the tree can have.

    If height = 0 then AVL tree can have one 1 node If height = 5 then AVL tree can have minimum 20 nodes

    Algorithm

    In an AVL tree, we have to maintain the height balance property, i.e. a difference in the height of the left and the right subtrees cannot be more than -1, 0 or 1 for each node. Using this property, we can create below recurrence relation −

    1. If height = 0 then return 1 2. If height = 1 then return 2 3. If height > 1 then return (1 + getMinAVLNodes(h – 1) + getMinAVLNodes(h – 2))

    Example

    #include <iostream> using namespace std; int getMinAVLNodes(int h){    if (h < 0) {       return 0;    }    if (h == 0 || h == 1) {       return h + 1;    }    return 1 + getMinAVLNodes(h - 1) + getMinAVLNodes(h -2); } int main(){    int h = 5;    cout << "Minimum nodes for " << h << " height = " << getMinAVLNodes(h) << endl;    return 0; }

    Output

    When you compile and execute the above program. It generates the following output −

    Minimum nodes for 5 height = 20

    What are the minimum number of nodes required for an AVL tree with height 7?

    Updated on 31-Oct-2019 07:15:22

    Prove a lower bound of 7 vertices:

    Since the tree is of height 3, it must have a path of length 3 from the root to a leaf, so we already have to have 4 vertices; $r-v_1-v_2-v_3$. Now r has to have balance factor in $\{1,0,-1\}$ (i.e. $|\text{BF}(r)|\leq1$) so it must have a path of length at least two on it's other subtree, so we have $r-v_4-v_5$ (at least two more vertices).

    For the same reasons (BF) $v_1$ has to have at least one child on it's other subtree, so we have $v_1-v_6$ (at least one more vertex).

    So we have proved that an AVL tree of height 3 has to have at least 7 vertices, your example shows that there is an AVL tree with 7 vertices, so the minimum is 7.

    The question is a bit old, but I've just studied the subject and so I can provide a detailed answer.

    If you just want the answer : The minimum of nodes in an AVL tree of height h is f(h+2) - 1 where f is the Fibonacci sequence, defined as follow : f(0) = 1, f(1) = 1, f(n+2) = f(n+1) + f(n).

    Proof :

    We can prove this relationship by proving the two following proposition by recursion :

    Let s(n) be the minimum number of nodes in an AVL tree of height n.

    We have : s(0) = 1, s(1) = 2, s(n+2) = s(n+1) + s(n) + 1.

    Proposition 1 : s(n) = f(0) + f(1) + f(2) + ... + f(n)

    This can quite easily be showed by recursion :

    First, at rank 0 and 1, we have : s(0) = f(0), s(1) = 2 = f(0) + f(1)

    Now let's show that if the propostion is true at rank n and n+1, then it is true at rank n+2 :

    s(n+2) = s(n+1) + s(n) + 1 [formula for s(n+2)] = (f(0) + f(1) + ... + f(n+1)) + (f(0) + f(1) + ... + f(n)) + 1 [apply Proposition 1 at rank n and n+1] = f(0) + ((f(1) + f(0)) + (f(2) + f(1)) + ... + (f(n+1) + f(n))) + 1 [rearrange the sums] = f(0) + (f(2) + f(3) + ... + f(n+2)) + 1 [apply f(n+2) = f(n+1) + f(n)] = f(0) + f(1) + (f(2) + f(3) + ... + f(n+2)) [f(1) = 1 and rearrange] = f(0) + f(1) + ... + f(n+2)

    Which by recursion shows that the property is true for any n greater than 0.

    Proposition 2 : f(n+2) - 1 = f(0) + f(1) + f(2) + ... + f(n)

    This proof is left as an exercice to the reader, but it's exactly the same principle as the previous one.

    Now we have, by applying props 1 and 2 successively : s(n) = f(0) + f(1) + ... + f(n) = f(n+2) - 1

    There is a general expression for the Fibonacci sequence, so we can apply it for n+2 to very quickly compute the minimum number of nodes in an AVL tree of any height.

    Given the height of an AVL tree ‘h’, the task is to find the minimum number of nodes the tree can have.

    Examples : 

    Input : H = 0 Output : N = 1 Only '1' node is possible if the height of the tree is '0' which is the root node. Input : H = 3 Output : N = 7

    Recursive Approach : In an AVL tree, we have to maintain the height balance property, i.e. difference in the height of the left and the right subtrees can not be other than -1, 0 or 1 for each node. 

    We will try to create a recurrence relation to find minimum number of nodes for a given height, n(h). 

    • For height = 0, we can only have a single node in an AVL tree, i.e. n(0) = 1
    • For height = 1, we can have a minimum of two nodes in an AVL tree, i.e. n(1) = 2
    • Now for any height ‘h’, root will have two subtrees (left and right). Out of which one has to be of height h-1 and other of h-2. [root node excluded]
    • So, n(h) = 1 + n(h-1) + n(h-2) is the required recurrence relation for h>=2 [1 is added for the root node]

    Below is the implementation of the above approach:  

        return (1 + AVLnodes(height - 1) + AVLnodes(height - 2));

        cout << AVLnodes(H) << endl;

    static int AVLnodes(int height)

        return (1 + AVLnodes(height - 1) + AVLnodes(height - 2));

    public static void main(String args[])

        System.out.println(AVLnodes(H));

        return (1 + AVLnodes(height - 1) +

    if __name__ == '__main__':

    static int AVLnodes(int height)

        return (1 + AVLnodes(height - 1) +

    public static void Main()

        Console.Write(AVLnodes(H));

    function AVLnodes($height)

        return (1 + AVLnodes($height - 1) +

    function AVLnodes(height)

        return (1 + AVLnodes(height - 1) +

    document.write(AVLnodes(H));

    Tail Recursive Approach :  

    • The recursive function for finding n(h) (minimum number of nodes possible in an AVL Tree with height ‘h’) is n(h) = 1 + n(h-1) + n(h-2) ; h>=2 ; n(0)=1 ; n(1)=2;
    • To create a Tail Recursive Function, we will maintain 1 + n(h-1) + n(h-2) as function arguments such that rather than calculating it, we directly return its value to main function.

    Below is the implementation of the above approach :  

    int AVLtree(int H, int a = 1, int b = 2)

        return AVLtree(H - 1, b, a + b + 1);

        cout << "n(" << H << ") = "

    static int AVLtree(int H, int a, int b)

        return AVLtree(H - 1, b, a + b + 1);

    public static void main(String[] args)

        int answer = AVLtree(H, 1, 2);

        System.out.println("n(" + H + ") = " + answer);

        return AVLtree(H - 1, b, a + b + 1);

    if __name__ == '__main__':

        answer = AVLtree(H, 1, 2);

    static int AVLtree(int H, int a, int b)

        return AVLtree(H - 1, b, a + b + 1);

    public static void Main(String[] args)

        int answer = AVLtree(H, 1, 2);

        Console.WriteLine("n(" + H + ") = " + answer);

        function AVLtree(H, a, b)

            return AVLtree(H - 1, b, a + b + 1);

        let answer = AVLtree(H, 1, 2);

        document.write("n(" + H + ") = " + answer);


    Article Tags :