Minimum number of nodes in an AVL Tree with given height - GeeksforGeeks

18 Jun, 2021
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:  
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to find// minimum number of nodesint AVLnodes(int height){    // Base Conditions    if (height == 0)        return 1;    else if (height == 1)        return 2;     // Recursive function call    // for the recurrence relation    return (1 + AVLnodes(height - 1) + AVLnodes(height - 2));} // Driver Codeint main(){    int H = 3;    cout << AVLnodes(H) << endl;}
// Java implementation of the approach class GFG{      // Function to find// minimum number of nodesstatic int AVLnodes(int height){    // Base Conditions    if (height == 0)        return 1;    else if (height == 1)        return 2;      // Recursive function call    // for the recurrence relation    return (1 + AVLnodes(height - 1) + AVLnodes(height - 2));}  // Driver Codepublic static void main(String args[]){    int H = 3;    System.out.println(AVLnodes(H));}}
# Python3 implementation of the approach # Function to find minimum# number of nodesdef AVLnodes(height):         # Base Conditions    if (height == 0):        return 1    elif (height == 1):        return 2     # Recursive function call    # for the recurrence relation    return (1 + AVLnodes(height - 1) +                AVLnodes(height - 2)) # Driver Codeif __name__ == '__main__':    H = 3    print(AVLnodes(H))     # This code is contributed by# Surendra_Gangwar
// C# implementation of the approachusing System; class GFG{     // Function to find// minimum number of nodesstatic int AVLnodes(int height){    // Base Conditions    if (height == 0)        return 1;    else if (height == 1)        return 2;     // Recursive function call    // for the recurrence relation    return (1 + AVLnodes(height - 1) +                AVLnodes(height - 2));} // Driver Codepublic static void Main(){    int H = 3;    Console.Write(AVLnodes(H));}} // This code is contributed// by Akanksha Rai
<?php// PHP implementation of the approach // Function to find minimum// number of nodesfunction AVLnodes($height){    // Base Conditions    if ($height == 0)        return 1;    else if ($height == 1)        return 2;     // Recursive function call    // for the recurrence relation    return (1 + AVLnodes($height - 1) +                AVLnodes($height - 2));} // Driver Code$H = 3;echo(AVLnodes($H)); // This code is contributed// by Code_Mech.
<script> // Javascript implementation of the approach // Function to find// minimum number of nodesfunction AVLnodes(height){         // Base Conditions    if (height == 0)        return 1;    else if (height == 1)        return 2;     // Recursive function call    // for the recurrence relation    return (1 + AVLnodes(height - 1) +                AVLnodes(height - 2));} // Driver codelet H = 3; document.write(AVLnodes(H)); // This code is contributed by decode2207 </script>
7
 
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 :  
C++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <iostream>using namespace std; // Function to return//minimum number of nodesint AVLtree(int H, int a = 1, int b = 2){    // Base Conditions    if (H == 0)        return 1;    if (H == 1)        return b;     // Tail Recursive Call    return AVLtree(H - 1, b, a + b + 1);} // Driver Codeint main(){    int H = 5;    int answer = AVLtree(H);     // Output the result    cout << "n(" << H << ") = "         << answer << endl;    return 0;}
// Java implementation of the approachclass GFG{ // Function to return//minimum number of nodesstatic int AVLtree(int H, int a, int b){    // Base Conditions    if (H == 0)        return 1;    if (H == 1)        return b;     // Tail Recursive Call    return AVLtree(H - 1, b, a + b + 1);} // Driver Codepublic static void main(String[] args){    int H = 5;    int answer = AVLtree(H, 1, 2);     // Output the result    System.out.println("n(" + H + ") = " + answer);}} // This code is contributed by PrinciRaj1992
# Python3 implementation of the approach # Function to return# minimum number of nodesdef AVLtree(H, a, b):         # Base Conditions    if(H == 0):        return 1;    if(H == 1):        return b;     # Tail Recursive Call    return AVLtree(H - 1, b, a + b + 1); # Driver Codeif __name__ == '__main__':    H = 5;    answer = AVLtree(H, 1, 2);     # Output the result    print("n(", H , ") = "\        , answer); # This code is contributed by 29AjayKumar
// C# implementation of the approachusing System;     class GFG{ // Function to return//minimum number of nodesstatic int AVLtree(int H, int a, int b){    // Base Conditions    if (H == 0)        return 1;    if (H == 1)        return b;     // Tail Recursive Call    return AVLtree(H - 1, b, a + b + 1);} // Driver Codepublic static void Main(String[] args){    int H = 5;    int answer = AVLtree(H, 1, 2);     // Output the result    Console.WriteLine("n(" + H + ") = " + answer);}} // This code is contributed by Princi Singh
<script>    // Javascript implementation of the approach         // Function to return    //minimum number of nodes    function AVLtree(H, a, b)    {        // Base Conditions        if (H == 0)            return 1;        if (H == 1)            return b;         // Tail Recursive Call        return AVLtree(H - 1, b, a + b + 1);    }         let H = 5;    let answer = AVLtree(H, 1, 2);       // Output the result    document.write("n(" + H + ") = " + answer); // This code is contributed by mukesh07.</script>
n(5) = 20
 
krikti
tufan_gupta2000
SURENDRA_GANGWAR
Akanksha_Rai
Code_Mech
princiraj1992
princi singh
29AjayKumar
decode2207
mukesh07
AVL-Tree
Self-Balancing-BST
Binary Search Tree
Binary Search Tree
AVL-Tree
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
set vs unordered_set in C++ STL
Floor and Ceil from a BST
Check if a triplet with given sum exists in BST
Construct BST from given preorder traversal | Set 2
Convert BST to Min Heap
Print BST keys in the given range
Red Black Tree vs AVL Tree
Optimal sequence for AVL tree insertion (without any rotations)
Construct a Binary Search Tree from given postorder
Find the largest BST subtree in a given Binary Tree | Set 1