The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+28 votes
4.8k views

We are given a set of $n$ distinct elements and an unlabeled binary tree with $n$ nodes. In how many ways can we populate the tree with the given set so that it becomes a binary search tree?

  1. $0$
  2. $1$
  3. $n!$
  4. $\frac{1} {n+1} .^{2n}C_n$
asked in DS by Veteran (106k points)
recategorized by | 4.8k views
–4

what about this?This is the same structure and can be populated in two different ways.

for 15 nodes BST can be populated in 8 ways.

0
The 2nd pic is not a BST.so your counting process is wrong.
+1
in 2nd pic.- Every element of right subtree should be greater than 5 but here 4 is present so it is wrong.
0
he said an unlabeled binary means out of all possible unlabel binary tree we took on of them then they ask how many way u form bst. so only one way
0
0
if it is given a labeled binary tree with n nodes then???

7 Answers

+34 votes
Best answer

With $n$ nodes, there are $\frac{^{2n}Cn}{(n+1)}$ distinct tree structures possible.

Corresponding to each structure, only one binary search tree (BST) can be formed because inorder is fixed.

Here, we are already given one such structure therefore only one tree possible.

If binary trees would have been asked, $n!$ trees would have been possible corresponding to each distinct tree structure. Here, tree structure is fixed and hence, we can have only one possibility for BST as elements are distinct. For general cases:
http://gatecse.in/wiki/Number_of_Binary_trees_possible_with_n_nodes 

answered by Loyal (8.1k points)
edited by
0
This is the correct explanation.
0

the word structure is nowhere mentioned in the question. they have used the word set. 

0
word set is given means it contains some  value so there is only 1 way to put the value into the node(means populate the tree with the given set)  as already structure is fixed for BST.
0
Structure means the unlabeled binary tree.
0
@sikha if binary trees would have been asked how it is n! It could be more than it..

This could be true for a complete binary tree or almost CBT
0
Sir as you say that inorder is given

can you please tell me how.
+26 votes
Given binary tree is unlabeled . So as it is given we are not allowed to change the formation of tree. Then To make it BST we can use atmost 1 way . As for particular structure we can not use n! arrangement of nodes (Becasue they are labeled and it is BST not BT)
answered by Active (1.5k points)
+11
Yes. Here tree structure is fixed and hence we can have only 1 possibility for BST as elements are distinct. For general cases:
http://gatecse.in/wiki/Number_of_Binary_trees_possible_with_n_nodes
+1
So for binary trees, the answer would have been n! ?
0
what is think about option D
0
@Arjun where they said structure is fixed? They did,'t use the word structure. They have just given the set.
0

In the question they have asked "In how many ways can we populate the tree with the given set ...". Since it is particularly mentioned "populate the tree", hence we have to set values to that structure(given unlabeled binary tree) only and it can be done by 1 way only so, that given unlabeled binary tree turns into a BST.

+5 votes

With n nodes there are 2nCn/(n+1) different structure. One structure out of these 2nCn/(n+1) structures is given.

Now no of ways to fill numbers in given structure to ensure BST property is one and only one.

http://gatecse.in/wiki/Number_of_Binary_trees_possible_with_n_distinct_nodes

answered by Veteran (55.6k points)
0
In BST left nodes have value less than root and right nodes have value greater than root. SO we could have more than one BST if we take different node for root there will be more than one BST right!!!
0
Take any structure and try to fill that particular structure with these n distinct keys. There will be one and only one sequence possible for a particular structure.
+5 votes
Take any structure formed by n unlabeled binary tree ... suppose n=3 ... then try to label it which will be n! (here 3!) ... then U will find only one structure satisfying the conditions of BST .. So B is the answer ...
answered by Boss (11.6k points)
+2

Logic of BST

+3 votes
Just the simple logic is that we have 2nCn/(n+1) unlabelled trees , now each unlabelled tree will always correspond to one BST , consider 1,2,3 now say you draw a right skewed unlabelled tree , now u can label it only in one way 1 2 3 to form a BST , if say u draw a left skewed unlabelled tree , then u can label it as 3 2 1 , to form BST therefore each unlabelled tree gives rise to only 1 BST.

 

But in the question it is already given that we are provided with one unlabelled tree therefore there is only 1 way in which we can populate it to form a BST therefore answer is option A .
answered by Loyal (7.7k points)
0
In the question, the tree is also given.
0
Thankss sir for correction I have edited my answer .
0 votes

i found all the unlabeled trees can be binary search trees. Please check this and correct if iam wrong.Guys, please check whether it is correct or not.

answered by (19 points)
–3 votes
Ans is D
answered by Boss (10.3k points)
0

Structure is fixed here. So, we can have only 1 way of filling as elements are distinct. 

http://gatecse.in/wiki/Number_of_Binary_trees_possible_with_n_distinct_nodes

0
yes as the elements are distinct so for each structure only one order is possible .
0
Can you please explain what is wrong in my answer ?
Answer:

Related questions



Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

42,599 questions
48,600 answers
155,662 comments
63,733 users