4.3k views
The number of binary min. heaps that can be formed from a set of 7 distinct integers is _________?
retagged | 4.3k views
+3
0
@saurabh SIR,
Can u ps make an answer ?  I didnt understand the link :(
0
ok dont worry..... in which part u r facing problem tell me i ll explain u surely.....
0
I only undrstood upto filling them in breadfirst manner .   What we have to do after that ?  How did he got the equation ? Could u able to explain it in simple words taking this example . ?
+1
ok....
0
waiting :)
0
48 ans??
0
I did like this.

The height of the heap is 2.

Since it is a minheap, so root has only one choice ie the minimum element.

At height 2, we have the 4 maximum elements, which have 4! possibilities.

At height 1, we have remaining 2 elements, which can be arranged in 2! ways.

So, 4! * 2! = 48 min heaps.
0

There are more possibilities: Suppose elements are (1234567)

level 1 -     1

level 2 -   2, 5

level 3 -  3,4  6,7

(u have choose only 2 and 3 in level 2)
0
aPPROACH GIVEN IN ABOVE LINK IS DIFFERENT FROM YOURS RIGHT ?

Why have you multiplied 4! * 2 !  didnt understand this part in ur explanation
0
answer given is 80 donno how even i got 48
0
Yes @target2017

There are more possibilities.
0
@gateset @jithin see my ans bellow....
0
yes I got it thank u so much great explanation :)

• Pick any $7$ distinct  integers and sort it .
• After sorting, pick the minimum element and make it the root of the min heap. So, there is only $1$ way to make the root of the min heap.
• Now we are left with $6$ elements.
• Total ways to design a min heap from $6$ elements = $C(6,3) * 2! * C(3,3) * 2!$ = $80$

$C(6,3) * 2!$ :- Pick up any $3$ elements for the left subtree and each left subtree combination can be permuted in $2!$ ways by interchanging the children and similarly, for right subtree .

How many min heaps for $N = 15$ elements ?

• Pick any $15$ distinct  integers and sort it .
• After sorting, pick the minimum element and make it the root of the min heap. So, there is only $1$ way to make the root of the min heap.
• Now we are left with $14$ elements.
• Total ways to design a min heap from $14$ elements

=> $C(14,7) * \left ( C(6,3) * 2! * C(3,3) * 2! \right ) * C(7,7) * \left ( C(6,3) * 2! * C(3,3) * 2! \right )$

$C(14,7) * \left ( C(6,3) * 2! * C(3,3) * 2! \right )$ :- Pick up any $7$ elements for the left subtree and fix the root. Now, we are left with $6$ elements and using the same procedure for above problem, take each left subtree combination can be permuted in $2!$ ways by interchanging the children and similarly, for right subtree .

selected by
0
Any signififance of C(6,3) in general case ?

ie in n = N  then
C((N-1) , (N-1)/2 ) * 2!  * C((N-1)/2 , (N-1)/2 )   when n is odd  . Can i generalize like this ?
+1
Yes, some generalization is there . I've edited. You can check now !!
+1
What if N is EVEN ?
0
Well, same logic. Basic idea is to design a complete binary tree and everything will be the same .
+12

for n=7

for n=8

+2
Right @pC :)
0
How do you calculate for n=5 @Kapil Veteran
0
Can you try and tell me ? Logic above remains the same .
0
Yes,got it.initially i thought that i should divide the n-1 integers into two halves,but later i found out the property of heap.So,i got it now.Answer was great but it would be more helpful if you have also added how to divide n-1 integers into L and R.Thanks again for the answer :)

for n=5

n-1=4

here by heap property L=3 and R=1

So,f(5)=4C3 * f(3) * f(1)
0
I have a doubt that , according to which property of heap , did you make L=3 and R=1 ??
0
According to the heap property first fill left side of subtree then right side of subtree...that means always fill by left side only.
0

dont you think for 15 it should be (2!*C(14,7)∗(C(6,3)∗2!∗C(3,3)∗2!)∗ 2!*C(7,7)∗(C(6,3)∗2!∗C(3,3)∗2!))*2

because on every level we can exchange children.??

0

your method is not correct  watch the below line

C(14,7)∗(C(6,3)∗C(2,1)∗C(3,3)∗C(2,1))∗C(7,7)∗(C(6,3)∗C(2,1)∗C(3,3)∗C(2,1))   from this line you can understand why we used 2!

0

C(14,7)∗(C(6,3)∗2!∗C(3,3)∗2!)∗C(7,7)∗(C(6,3)∗2!∗C(3,3)∗2!)

shouldn't this be multiplied by 2!

0
why you want to multiply by 2!
0

Children of the roots can be swapped irrespective of each other. So right subtree can be a left subtree and vice-versa. @Gurdeep Saini

0
this thing has been included in C(14,7)
means left child of rootnode  can be any key from these 14 keys
multiply by 2! will be extra count
0

C(6,3) * 2!

here we are multiplying by 2! since we can permute left and right sub tree!

@Gurdeep Saini M i Wrong?

0
here we are using this because

C(14,7)∗(C(6,3)∗C(2,1)∗C(3,3)∗C(2,1))∗C(7,7)∗(C(6,3)∗C(2,1)∗C(3,3)∗C(2,1))

here 2!=2C1   means we are choosing one key from two and assign it as left key and remaining one will be right

Here i m generalizing it with an  example....
suppose i have a min heap like this

assume root at level zero hight of this heap is k=3
The smallest element has to be placed at the root of the heap.  After that we split up the remaining elements into the two subtrees and recursively compute the number of binary heaps that they can make.  The trickiest part here is that the two subtrees don't have necessarily have the same number of nodes (unless N is 1 less than a power of two).

We fill the tree in breadth first order which means that nodes in the lowest level will go to the left subtree first and then any excess nodes will be in the right subtree.
suppose that at last level there r m nodes nd total nodes=n
hence n=2k-1 +m
in above tree k=3 m=7 so n=2k-1 +m=8-1+7=14
or we can say that m=n-2k+1
now calculate number of elements in the left subtree as L ?
we now that before last level every level is complete and nodes r equally distributed into left subtree nd .right subtree.
so no of nodes befor last level in  left subtree or.right subtree.=p
now we know that befor last level total nodes=2k-1 now leave root now total nodes=2k-2
these total nodes 2k-2, is equally distributed into left subtree nd .right subtree.
hence p=(2k-2)/2=2k-1 -1
now suppose number of elements in the left subtree as L and number of elements in the right subtree as L
L=p+min(2k-1,m )        R=p+max(0,m-2k-1)
expalanation for  L=p+min(2k-1,m )        R=p+max(0,m-2k-1)
For L=p+min(2k-1,m )        R=p+max(0,m-2k-1
there r 2 cases
case-1
if our lst part is not complete then it ll surely have m elements in left sub tre part=no of nodes at last level so if all nodes at last level comes in lst part so there r o nodes in rst part it gives
L=p+min(   ,m )        R=p+max(0,   )
case 2
if our lst part is  complete nd there r some nodes also in rst part
so for lst part 2k/2=2k-1 nodes in lst part at last level nd rest nodes out of m ie m-2k-1 will be on rst part at last level
L=p+min(2k-1,    )        R=p+max(   ,m-2k-1
now by cobining case 1&2
L=p+min(2k-1,m )        R=p+max(0,m-2k-1)

Returning to the original problem you can calculate f(n), the number of binary heaps with n distinct elements, as f(n)
f(0)=1, f(1)=1
f(n)=n-1CLf(L)f(R)
this is generating series (1, 1, 1, 2, 3, 8, 20, 80, 210, 896, 3360, 19200, 79200, 506880, 2745600)
http://oeis.org/A056971
f(7)=80

----------------------------------------------------------------------------------------------------------------------------------------------------------------
if we have our min heap as complete binary tree then this method makes it mor simpler bcoz for a complete binary tree for every node its left sub tree nd right sub tree  is also complete binary tree with same no of nodes in lst and rst  nd they have also same no of binary min heaps means f(L)=f(R)
suppose n=15 means it is complete as 24-1=15, k=3
L=R=2k-1=7
f(15)=14c7 *(f(7))2
f(7)=6c3*(f(3))2
f(3)=2c1*(f(1))2
f(1)=1

edited
0
Thank you so much :)
+3
if u have little bit doubt also u can ask
btw i m nt sir.... :)
+1

How you got
L=p+min(2k-1,m )        R=p+max(0,m-2k-1)

Equation ?

I understood upto p=(2k-2)/2=2k-1 -1 statement .

You could have explained it using the same example till the last  instead of that series . The traditional way just to understand this topic
This is such a nice explanation.

Thanks :)

+2

For L=p+min(2k-1,m )        R=p+max(0,m-2k-1)
there r 2 cases
case-1
if our lst part is not complete then it ll surely have m elements in left sub tre part=no of nodes at last level so if all nodes at last level comes in lst part so there r o nodes in rst part it gives
L=p+min(   ,m )        R=p+max(0,   )
case 2
if our lst part is  complete nd there r some nodes also in rst part
so for lst part 2k/2=2k-1 nodes in lst part at last level nd rest nodes out of m ie m-2k-1 will be on rst part at last level
L=p+min(2k-1,    )        R=p+max(   ,m-2k-1)
now by cobining case 1&2
L=p+min(2k-1,m )        R=p+max(0,m-2k-1)

0
how f(0) = 1?

Ans is 80

As the smallest element will be at root and for left sub tree from remaining 6 element we can pick any six element 6C3 and smallest element among 3 will be root and remaining element we can arrange by 2!

Similarly for right subtree 3C3*2!

So 6C3*2!*3C3*2!=80

Therefore total choice = 2*5*4=40

Same can be done on right subtree of root.

So there are total = 40 * 2 choices =80 Ans

0
In 2nd level

{2,3} is not only choice

it could be {2,3} or {2,4} or {2,5}

right?
0

No I dnt think 2nd level can store 4 or 5 otherwise it will violate min-heap property.

As  i can have

0
COUNTER EXAMPLE IS :-

LEVEL 0 : -                  1

LEVEL 1 :-            4            2

lEVEL 2 :-         5      7      3        6

1
2