给定AVL树的高度“ h”,任务是找到该树可以具有的最小节点数。
例子 :
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
递归方法:在AVL树中,我们必须保持高度平衡属性,即,每个子树的左右子树的高度差不能为-1、0或1。
我们将尝试创建一个递归关系,以找到给定高度n(h)的最小节点数。
- 对于height = 0,我们在AVL树中只能有一个节点,即n(0)= 1
- 对于height = 1,我们在AVL树中至少可以有两个节点,即n(1)= 2
- 现在,对于任何高度“ h”,根都将具有两个子树(左和右)。其中一个必须具有高度h-1,另一个必须具有h-2。 [排除根节点]
- 因此, n(h)= 1 + n(h-1)+ n(h-2)是h> = 2所需的递归关系[为根节点添加1]
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
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
# 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
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
C++
// C++ implementation of the approach
#include
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
输出:
7
尾递归方法:
- 查找n(h)(在高度为“ h”的AVL树中可能的最小节点数)的递归函数为n(h)= 1 + n(h-1)+ n(h-2); h> = 2; n(0)= 1; n(1)= 2;
- 要创建尾递归函数,我们将1 + n(h-1)+ n(h-2)保留为函数参数,这样我们无需将其值直接返回给main函数即可对其进行计算而不是对其进行计算。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
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
输出:
n(5) = 20