in DS retagged by
38,822 views
67 votes
67 votes
The number of possible min-heaps containing each value from $\{1,2,3,4,5,6,7\}$ exactly once is _______
in DS retagged by
by
38.8k views

4 Comments

Great explanation !
0
0
edited by

Solved by using permutations and Combinations(Look both Answer together  for better understanding and visualization)

https://gateoverflow.in/102171/min-heap?show=102229#a102229

https://gateoverflow.in/204121/gate-cse-2018-question-46?show=204445#a204445

0
0

14 Answers

82 votes
82 votes
Best answer

Lets answer this question in an easier way :

Now do $\frac{7!}{7 \times 3\times 3 }= 80$

Here $7!$ because $7$ items to be filled, Now $7$ because root has only $7$ nodes as its decedent including itself and only one can be the root. In same way we get $3$ and $3$ for the second level nodes and $1$ and $1$ for the third level. 

edited by

4 Comments

Really easy approach than many answers here!
0
0
eye opener. Thanks man
0
0
best explanation
0
0
379 votes
379 votes

Ans: 80


Explanation:

Number of min-heaps possible with keys $\{1, 2, 3, 4, 5, 6, 7\}$.

A min-heap is a binary tree such that.

- the data contained in each node is less than (or equal to) the data in that node's children.

- the binary tree is complete. 

Since a binary heap is always a complete binary tree, so with $7$ nodes, we can have $2$ levels (root at level $0$). It's structure will be like this:

Now because of min-heap property, every node's value must be smaller than all of it's children. So, this ensure that the minimum will always be at the root. $\therefore$ $1$ will be at the root.

The second minimum element(i.e. $2$) can be present at the first level only(root is at the zeroth level). So we have two options. Let's, for now, fix $2$ at the left side.

We are now left with $5$ more elements $\{3, 4, 5, 6, 7\}$. For the left subtree's two leaves, we have $5 * 4$ ways. i.e. first choosing one of the 5 nodes, then choosing one of the remaining 4 nodes. 

Now $3$ nodes left. Out of these $3$, one will be the least among them, and that will definitely become the parent of the two remaining leaves(in the right subtree). Now with $2$ nodes left, we can arrange them in $2$ ways.

This gives $(5 * 4) * 2 = 40$ ways.

We can have the same number of ways, if we fixed $2$ at the right subtree instead of left. So total ways:

$= 40 * 2$

$= \mathbf{80}$

edited by

4 Comments

@MohanK, Nice question. 

If you do 5C2, you are essentially choosing 2 out of 5. That means the ordering is not taken into account. i.e. {3,5} is same as {5,3}. But in our case, they should be treated differently as they will yield 2 different trees. 

Some more about this question, you can also think of the solution recursively, that will be an interesting problem to look at. In any case, I won’t recommend anyone to mug up any formula for such questions (like done in the selected answer). Mugging up tons of formulas will never work in GATE. Rather, try to create formulas on your own.

13
13
Got it !

Thanks for the quick response 😊
1
1
best answer! 🏆
0
0
76 votes
76 votes

1 has to be root. Now,

case 1: 2 and 3 are on 2nd level.

so, 4,5,6 and 7 can occupy any of 4 positions in the 3rd level as all are less than 2 and 3. So, 4! arrangements. And 2 and 3 can be arranged in 2 ways in 2nd level (mirror image). Hence 2*24=48

case 2: 2 and 4 are on 2nd level

Now, 3 can only be below 2 and not 4 as it is MIN heap. So, 2 cases are possible 3XXX or X3XX, which gives 3!+3!=12 arrangements. And 2 and 4 can be arranged in 2 ways in 2nd level (mirror image). Hence 2*(6+6)=24

case 3: 2 and 5 are on 2nd level

Now, 3 and 4 can only be below 2 and not 5 as it is MIN heap. So, 2 arrangements are possible for 3 and 4 and similarly 2 for 6 and 7. And 2 and 5 can be arranged in 2 ways in 2nd level (mirror image). Hence, 2*(2+2)=8

Hence, total 48+24+8=80

4 Comments

yes @Shaik Masthan is should be 2*2*2.

0
0
i was looking for the same approach
1
1
thanks man
0
0
62 votes
62 votes

Answer : 80

4 Comments

210 number of ways for 1,2,3,4,5,6,7,8 this set   

$\binom{7}{4}$*$\binom{3}{2}$*2

35*3*2

210   use above method fix 1 then we have 7 element for left subtree $\binom{7}{4}$ then fix that element now we have 3 l3mnt in left sub tree then $\binom{3}{2}$ Now go right subtree we only 3 element left out of those fix 1 elemt now we can 2 ways

0
0
Why the answer is not 420? Do we need to arrange two left nodes also?
0
0
Thanks a lot sir
0
0
Answer:

Related questions