Minimum number of nodes in an AVL Tree with given 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).

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to find
// minimum number of nodes
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 Code
int main()
{
    int H = 3;
    cout << AVLnodes(H) << endl;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
  
class GFG{
      
  
// Function to find
// minimum number of nodes
static 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 Code
public static void main(String args[])
{
    int H = 3;
    System.out.println(AVLnodes(H));
}
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 implementation of the approach
  
# Function to find minimum 
# number of nodes
def 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 Code
if __name__ == '__main__':
    H = 3
    print(AVLnodes(H))
      
# This code is contributed by
# Surendra_Gangwar
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG
{
      
// Function to find
// minimum number of nodes
static 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 Code
public static void Main()
{
    int H = 3;
    Console.Write(AVLnodes(H));
}
}
  
// This code is contributed 
// by Akanksha Rai
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the approach
  
// Function to find minimum
// number of nodes
function 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.
chevron_right

Output:

7

Tail Recursive Approach :

Below is the implementation of the above approach :

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <iostream>
using namespace std;
  
// Function to return 
//minimum number of nodes
int 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 Code
int main()
{
    int H = 5;
    int answer = AVLtree(H);
  
    // Output the result
    cout << "n(" << H << ") = "
         << answer << endl;
    return 0;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG
{
  
// Function to return 
//minimum number of nodes
static 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 Code
public 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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function to return 
# minimum number of nodes
def 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 Code
if __name__ == '__main__':
    H = 5;
    answer = AVLtree(H, 1, 2);
  
    # Output the result
    print("n(", H , ") = "\
        , answer);
  
# This code is contributed by 29AjayKumar
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
      
class GFG
{
  
// Function to return 
//minimum number of nodes
static 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 Code
public 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
chevron_right

Output:
n(5) = 20

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.





Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :