Minimum number of nodes in an AVL Tree with given height
Last Updated :
10 Apr, 2023
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++
// 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;
}
Java
// 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));
}
}
Python3
# Python3 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
C#
// 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
PHP
<?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.
JavaScript
<script>
// Javascript 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
let H = 3;
document.write(AVLnodes(H));
// This code is contributed by decode2207
</script>
Time complexity O(2^n),where n is the height of the AVL tree.the reason being the function recursively calls itself twice for each level of the tree, resulting in an exponential time complexity.
Space complexity O(n),where n is the height of the AVL tree.
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++
// 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;
}
Java
// 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
Python3
# 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
C#
// 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
JavaScript
<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>
Time complexity O(H),where H is the height of the AVL tree being calculated.
the function makes a recursive call for each level of the AVL tree, and the maximum number of levels in an AVL tree is H.
Space complexity O(1), which is constant.
Similar Reads
AVL Tree Data Structure
An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. The absolute difference between the heights of the left subtree and the right subtree for any node is known as the balance factor of
4 min read
What is AVL Tree | AVL Tree meaning
An AVL is a self-balancing Binary Search Tree (BST) where the difference between the heights of left and right subtrees of any node cannot be more than one. KEY POINTSIt is height balanced treeIt is a binary search treeIt is a binary tree in which the height difference between the left subtree and r
2 min read
Insertion in an AVL Tree
AVL tree is a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees cannot be more than one for all nodes. Example of AVL Tree: The above tree is AVL because the differences between the heights of left and right subtrees for every node are less than
15+ min read
Insertion, Searching and Deletion in AVL trees containing a parent node pointer
AVL tree is a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees cannot be more than one for all nodes. The insertion and deletion in AVL trees have been discussed in the previous article. In this article, insert, search, and delete operations are
15+ min read
Deletion in an AVL Tree
We have discussed AVL insertion in the previous post. In this post, we will follow a similar approach for deletion.Steps to follow for deletion. To make sure that the given tree remains AVL after every deletion, we must augment the standard BST delete operation to perform some re-balancing. Followin
15+ min read
How is an AVL tree different from a B-tree?
AVL Trees: AVL tree is a self-balancing binary search tree in which each node maintain an extra factor which is called balance factor whose value is either -1, 0 or 1. B-Tree: A B-tree is a self - balancing tree data structure that keeps data sorted and allows searches, insertions, and deletions in
1 min read
Practice questions on Height balanced/AVL Tree
AVL tree is binary search tree with additional property that difference between height of left sub-tree and right sub-tree of any node canât be more than 1. Here are some key points about AVL trees:If there are n nodes in AVL tree, minimum height of AVL tree is floor(log 2 n). If there are n nodes i
4 min read
AVL with duplicate keys
Please refer below post before reading about AVL tree handling of duplicates. How to handle duplicates in Binary Search Tree?This is to augment AVL tree node to store count together with regular fields like key, left and right pointers. Insertion of keys 12, 10, 20, 9, 11, 10, 12, 12 in an empty Bin
15+ min read
Count greater nodes in AVL tree
In this article we will see that how to calculate number of elements which are greater than given value in AVL tree. Examples: Input : x = 5 Root of below AVL tree 9 / \ 1 10 / \ \ 0 5 11 / / \ -1 2 6 Output : 4 Explanation: there are 4 values which are greater than 5 in AVL tree which are 6, 9, 10
15+ min read
Difference between Binary Search Tree and AVL Tree
Binary Search Tree:A binary Search Tree is a node-based binary tree data structure that has the following properties: The left subtree of a node contains only nodes with keys lesser than the nodeâs key.The right subtree of a node contains only nodes with keys greater than the nodeâs key.The left and
2 min read