📜  有序树上的组合

📅  最后修改于: 2021-06-25 18:31:35             🧑  作者: Mango

有序树是定向树,其中节点的子节点以某种方式排序。它是一棵有根的树,其中为每个顶点的子级指定了顺序。之所以称为“平面树”,是因为子级的排序等效于树在平面中的嵌入,其根在顶部,每个顶点的子级都低于该顶点。
可以将有序树进一步指定为标记的有序树和未标记的有序树。

先决条件:加泰罗尼亚语数字|二项式系数。

标记的有序树:标记的树是一棵树,其中为每个顶点分配一个从1到n的唯一编号。

如果T1和T2是有序树。然后,T1 = T2,否则T1 = T2。

未标记的有序树:未标记的树是每个顶点都未标记的树。下面给出的是可能的未标记的有3个顶点的有序树。

具有n个节点的未标记有序树的总数等于第(n – 1)个加泰罗尼亚语数。
下面给出的是可能的未标记的有4个节点的有序树。该图将作为接下来的一些结果的参考示例。

1.恰好有k叶的树数。
让我们考虑一下,我们有一个’n’边。然后,对具有“ k”片叶子的所有可能的有序树的解由下式给出:

2.这些树中度为d的节点总数。
让我们考虑一下,我们有一个’n’边。然后,度为“ d”的节点总数的解由下式给出:

3.根的度数为r的树数。
让我们考虑一下,我们有一个’n’边。然后,由根给出度为“ r”的所有可能的有序树的解由下式给出:

下面是使用二项式系数的上述组合函数的实现:

C++
// C++ code to find the number of ordered trees
// with given number of edges and leaves
#include 
using namespace std;
 
// Function returns value of
// Binomial Coefficient C(n, k)
int binomialCoeff(int n, int k)
{
    int C[n + 1][k + 1] = { 0 };
    int i, j;
 
    // Calculate value of Binomial
    // Coefficient in bottom up manner
    for (i = 0; i <= n; i++) {
        for (j = 0; j <= min(i, k); j++) {
 
            // Base Cases
            if (j == 0 || j == i)
                C[i][j] = 1;
 
            // Calculate value using
            // previously stored values
            else
                C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
        }
    }
 
    return C[n][k];
}
 
// Function to calculate the number
// of trees with exactly k leaves.
int k_Leaves(int n, int k)
{
    int ans = (binomialCoeff(n, k) * binomialCoeff(n, k - 1)) / n;
    cout << "Number of trees having 4 edges"
         << " and exactly 2 leaves : " << ans << endl;
    return 0;
}
 
// Function to calculate total number of
// nodes of degree d in these trees.
int numberOfNodes(int n, int d)
{
    int ans = binomialCoeff(2 * n - 1 - d, n - 1);
    cout << "Number of nodes of degree 1 in"
         << " a tree having 4 edges : " << ans << endl;
    return 0;
}
 
// Function to calculate the number of
// trees in which the root has degree r.
int rootDegreeR(int n, int r)
{
    int ans = r * binomialCoeff(2 * n - 1 - r, n - 1);
    ans = ans / n;
    cout << "Number of trees having 4 edges"
         << " where root has degree 2 : " << ans << endl;
    return 0;
}
 
// Driver program to test above functions
int main()
{
    // Number of trees having 3
    // edges and exactly 2 leaves
    k_Leaves(3, 2);
 
    // Number of nodes of degree
    // 3 in a tree having 4 edges
    numberOfNodes(3, 1);
 
    // Number of trees having 3
    // edges where root has degree 2
    rootDegreeR(3, 2);
 
    return 0;
}


Java
// java code to find the number of ordered
// trees with given number of edges and
// leaves
import java.io.*;
 
class GFG {
         
    // Function returns value of
    // Binomial Coefficient C(n, k)
    static int binomialCoeff(int n, int k)
    {
        int [][]C = new int[n+1][k+1];
        int i, j;
     
        // Calculate value of Binomial
        // Coefficient in bottom up manner
        for (i = 0; i <= n; i++) {
            for (j = 0; j <= Math.min(i, k); j++)
            {
     
                // Base Cases
                if (j == 0 || j == i)
                    C[i][j] = 1;
     
                // Calculate value using
                // previously stored values
                else
                    C[i][j] = C[i - 1][j - 1]
                               + C[i - 1][j];
            }
        }
     
        return C[n][k];
    }
     
    // Function to calculate the number
    // of trees with exactly k leaves.
    static int k_Leaves(int n, int k)
    {
        int ans = (binomialCoeff(n, k) *
                binomialCoeff(n, k - 1)) / n;
        System.out.println( "Number of trees "
             + "having 4 edges and exactly 2 "
                        + "leaves : " + ans) ;
        return 0;
    }
     
    // Function to calculate total number of
    // nodes of degree d in these trees.
    static int numberOfNodes(int n, int d)
    {
        int ans = binomialCoeff(2 * n - 1 - d,
                                       n - 1);
        System.out.println("Number of nodes "
           +"of degree 1 in a tree having 4 "
                          + "edges : " + ans);
        return 0;
    }
     
    // Function to calculate the number of
    // trees in which the root has degree r.
    static int rootDegreeR(int n, int r)
    {
        int ans = r * binomialCoeff(2 * n
                              - 1 - r, n - 1);
        ans = ans / n;
        System.out.println("Number of trees "
            + "having 4 edges where root has"
                      + " degree 2 : " + ans);
        return 0;
    }
     
    // Driver program to test above functions
 
    public static void main (String[] args)
    {
             
        // Number of trees having 3
        // edges and exactly 2 leaves
        k_Leaves(3, 2);
     
        // Number of nodes of degree
        // 3 in a tree having 4 edges
        numberOfNodes(3, 1);
     
        // Number of trees having 3
        // edges where root has degree 2
        rootDegreeR(3, 2);
    }
}
 
// This code is contributed by anuj_67.


Python3
# Python3 code to find the number of ordered
# trees with given number of edges and
# leaves
 
# Function returns value of
# Binomial Coefficient C(n, k)
def binomialCoeff(n, k):
     
    C = [[0 for i in range(k + 1)]
            for j in range(n + 1)]
     
    # Calculate value of Binomial
    # Coefficient in bottom up manner
    for i in range(n + 1):
        for j in range(min(i, k) + 1):
             
            # Base Cases
            if (j == 0 or j == i):
                C[i][j] = 1
 
            # Calculate value using
            # previously stored values
            else:
                C[i][j] = (C[i - 1][j - 1] +
                           C[i - 1][j])
                            
    return C[n][k]
 
# Function to calculate the number
# of trees with exactly k leaves.
def k_Leaves(n, k):
     
    ans = ((binomialCoeff(n, k) *
            binomialCoeff(n, k - 1)) // n)
    print("Number of trees ",
          "having 4 edges and exactly 2 ",
          "leaves : ", ans)
     
# Function to calculate total number of
# Nodes of degree d in these trees.
def numberOfNodes(n, d):
     
    ans = binomialCoeff(2 * n - 1 - d, n - 1)
    print("Number of Nodes ",
          "of degree 1 in a tree having 4 ",
          "edges : ", ans)
     
# Function to calculate the number of
# trees in which the root has degree r.
def rootDegreeR(n, r):
     
    ans = r * binomialCoeff(2 * n - 1 - r, n - 1)
    ans = ans // n
     
    print("Number of trees ",
          "having 4 edges where root has ",
          "degree 2 : ", ans)
     
# Driver code
if __name__ == '__main__':
     
    # Number of trees having 3
    # edges and exactly 2 leaves
    k_Leaves(3, 2)
 
    # Number of Nodes of degree
    # 3 in a tree having 4 edges
    numberOfNodes(3, 1)
 
    # Number of trees having 3
    # edges where root has degree 2
    rootDegreeR(3, 2)
 
# This code is contributed by aashish1995


C#
// C# code to find the number of ordered
// trees with given number of edges and
// leaves
using System;
 
class GFG {
         
    // Function returns value of
    // Binomial Coefficient C(n, k)
    static int binomialCoeff(int n, int k)
    {
        int [,]C = new int[n+1,k+1];
        int i, j;
     
        // Calculate value of Binomial
        // Coefficient in bottom up manner
        for (i = 0; i <= n; i++) {
            for (j = 0; j <= Math.Min(i, k); j++)
            {
     
                // Base Cases
                if (j == 0 || j == i)
                    C[i,j] = 1;
     
                // Calculate value using
                // previously stored values
                else
                    C[i,j] = C[i - 1,j - 1]
                            + C[i - 1,j];
            }
        }
     
        return C[n,k];
    }
     
    // Function to calculate the number
    // of trees with exactly k leaves.
    static int k_Leaves(int n, int k)
    {
        int ans = (binomialCoeff(n, k) *
                binomialCoeff(n, k - 1)) / n;
        Console.WriteLine( "Number of trees "
            + "having 4 edges and exactly 2 "
                        + "leaves : " + ans) ;
        return 0;
    }
     
    // Function to calculate total number of
    // nodes of degree d in these trees.
    static int numberOfNodes(int n, int d)
    {
        int ans = binomialCoeff(2 * n - 1 - d,
                                    n - 1);
        Console.WriteLine("Number of nodes "
        +"of degree 1 in a tree having 4 "
                        + "edges : " + ans);
        return 0;
    }
     
    // Function to calculate the number of
    // trees in which the root has degree r.
    static int rootDegreeR(int n, int r)
    {
        int ans = r * binomialCoeff(2 * n
                            - 1 - r, n - 1);
        ans = ans / n;
        Console.WriteLine("Number of trees "
            + "having 4 edges where root has"
                    + " degree 2 : " + ans);
        return 0;
    }
     
    // Driver program to test above functions
 
    public static void Main ()
    {
             
        // Number of trees having 3
        // edges and exactly 2 leaves
        k_Leaves(3, 2);
     
        // Number of nodes of degree
        // 3 in a tree having 4 edges
        numberOfNodes(3, 1);
     
        // Number of trees having 3
        // edges where root has degree 2
        rootDegreeR(3, 2);
    }
}
 
// This code is contributed by anuj_67.


PHP


输出:
Number of trees having 4 edges and exactly 2 leaves : 3
Number of nodes of degree 1 in a tree having 4 edges : 6
Number of trees having 4 edges where root has degree 2 : 2

时间复杂度: O(n * k)。
辅助空间: O(n * k)。

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。