Given n nodes. Find the number of different structural
binary trees that can be formed using the nodes.
Answers were Sorted based on User's Feedback
Answer / hitesh viradiya
m = 1 2 3 4 5 6 7 8
n=1 1
2 1 1
3 2 2 1
4 5 5 3 1
5 14 14 9 4 1
6 42 42 28 14 5 1
7 132 132 90 48 20 6 1
8 429 429 297 165 75 27 7 1
(2n)!/[(n+1)!n!]
Is This Answer Correct ? | 135 Yes | 16 No |
Answer / kathir
There are 2 pointers available for each node.
So we can have 2*n pointers totally.
Total no. of edges = n-1
So, Null pointers = n+1.
We need to choose (n-1) pointers from 2n pointers.
So the combination results in (2n)C(n-1).
We can have n distinct roots possible.
So, answer will be (2n) C (n-1) / n.
which leads to,
{2n C n}/{n+1}. ( Unlabelled )
Labelled Structured tree will be,
{2n C n}/{n+1} * {n!}
Is This Answer Correct ? | 30 Yes | 6 No |
No. of labeled binary tree :
n^(n-2)
No. unlabeled binary tree :
(2n)!/[(n+1)!.n!] (this is known as catlon number)
Is This Answer Correct ? | 25 Yes | 12 No |
Answer / atul barve
No. of labeled binary tree :
{(2n)!/[(n+1)!.n!]}*n!
No. unlabeled binary tree :
(2n)!/[(n+1)!.n!] (this is known as catlon number)
Is This Answer Correct ? | 19 Yes | 11 No |
Answer / fawad ghafoor
The number of different trees with 8 nodes is 248
2^n - n
Is This Answer Correct ? | 5 Yes | 0 No |
Answer / ajeet
int countTrees(int num)
{
if(num<=1)
return 1;
else
{
int root,left,right,sum=0;
for(root=1;root<=num;root++)
{
left=countTrees(root-1);
right=countTrees(num-root);
sum+=left*right;
}
return sum;
}
}
Is This Answer Correct ? | 5 Yes | 1 No |
Answer / sayandip ghosh
The max number of binary trees that can be formed from n
nodes is given by the Catlan Number C(n).
C(n) = (2n)! / (n+1)!*n! for n>=0.
int findNoTree(int low ,int high)
{
int sum=0;
if(low<=high)
{
for(int k=low;k<=high;k++)
{
if(k==low)
sum+=findNoTree(low+1,high);
else
{
if(k==high)
sum+=findNoTree(low,high-1);
else
sum=sum+findNoTree(low,k-1)*findNoTree(k+1,high);
}
}
return sum;
}
return 1;
}
Is This Answer Correct ? | 2 Yes | 1 No |
posted by surbhi just now main() { float a = 5.375; char *p; int i; p=(char*)&a; for(i=0;i<=3;i++) printf("%02x",(unsigned char) p[i]); } how is the output of this program is :: 0000ac40 please let me know y this output has come
Give a oneline C expression to test whether a number is a power of 2?
25 Answers EA Electronic Arts, Google, Motorola,
write the function. if all the character in string B appear in string A, return true, otherwise return false.
How to read a directory in a C program?
main() { int i=5; printf("%d%d%d%d%d%d",i++,i--,++i,--i,i); }
Write a program that produces these three columns sequence nos. using loop statement Sequence nos. Squared Squared + 5 1 1 6 2 4 9 3 9 14 4 16 21 5 25 30
main() { char * strA; char * strB = I am OK; memcpy( strA, strB, 6); } a. Runtime error. b. I am OK c. Compile error d. I am O
main() { int x=5; clrscr(); for(;x==0;x--) { printf("x=%d\nā", x--); } } a. 4, 3, 2, 1, 0 b. 1, 2, 3, 4, 5 c. 0, 1, 2, 3, 4 d. none of the above
main() { int i = 0xff ; printf("\n%d", i<<2); } a. 4 b. 512 c. 1020 d. 1024
Write a C function to search a number in the given list of numbers. donot use printf and scanf
What is full form of PEPSI
void main() { int i=10, j=2; int *ip= &i, *jp = &j; int k = *ip/*jp; printf(ā%dā,k); }