Combinatorics on ordered trees | Function returns value of Binomial Coefficient C ( n , k ) ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; Function to calculate the number of trees with exactly k leaves . ; Function to calculate total number of nodes of degree d in these trees . ; Function to calculate the number of trees in which the root has degree r . ; Number of trees having 3 edges and exactly 2 leaves ; Number of nodes of degree 3 in a tree having 4 edges ; Number of trees having 3 edges where root has degree 2
function binomialCoeff ( n , k ) { let C = new Array ( n + 1 ) ; let i , j ; for ( i = 0 ; i <= n ; i ++ ) { C [ i ] = new Array ( k + 1 ) ; for ( j = 0 ; j <= k ; j ++ ) { C [ i ] [ j ] = 0 ; } } for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= Math . min ( i , k ) ; j ++ ) { if ( j == 0 j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } return C [ n ] [ k ] ; } function k_Leaves ( n , k ) { let ans = parseInt ( ( binomialCoeff ( n , k ) * binomialCoeff ( n , k - 1 ) ) / n , 10 ) ; document . write ( " " + " " + " " + ans + " " ) ; return 0 ; } function numberOfNodes ( n , d ) { let ans = binomialCoeff ( 2 * n - 1 - d , n - 1 ) ; document . write ( " " + " " + " " + ans + " " ) ; return 0 ; } function rootDegreeR ( n , r ) { let ans = r * binomialCoeff ( 2 * n - 1 - r , n - 1 ) ; ans = parseInt ( ans / n , 10 ) ; document . write ( " " + " " + " " + ans + " " ) ; return 0 ; } k_Leaves ( 3 , 2 ) ; numberOfNodes ( 3 , 1 ) ; rootDegreeR ( 3 , 2 ) ;