12 votes 12 votes The number of possible min-heaps containing each value from $\{1,2,3,4,5,6,7,8\}$ exactly once is __________ DS go2025-mockgate-2 numerical-answers heaps + – gatecse asked Jan 17, 2021 • edited Jan 17, 2021 by Lakshman Bhaiya gatecse 748 views answer comment Share Follow See all 0 reply Please log in or register to add a comment.
Best answer 13 votes 13 votes We need $4$ levels for $8$ nodes in a min-heap. This means one sub-tree of root will have $4$ nodes and other one $3$ nodes. Now, number of min-heaps with $4$ nodes $=3$ and that with $3$ nodes $ = 2.$ Since $4!/3 = 8$ and $3!/2 = 3$ to find the number of min-heaps with $8$ nodes we can divide $8!$ first with $8$ -- since root is fixed instead of having $8$ ways and then by $8$ and $3.$ Thus, number of min-heaps with $8$ nodes $ = \dfrac{8!}{8.8.3} = \dfrac{7!}{24} = 210$ Reference: https://cs.stackexchange.com/questions/6456/how-many-different-max-heaps-exist-for-a-list-of-n-integers gatecse answered Jan 17, 2021 • selected Dec 25, 2021 by Arjun gatecse comment Share Follow See 1 comment See all 1 1 comment reply awesome_ash commented Jan 24 reply Follow Share Can we please get a detailed explanation? Like something with visuals and description as to why did you divide 4! by 3 and similarly, 8!/8? 2 votes 2 votes Please log in or register to add a comment.
12 votes 12 votes root will take the smallest one.so remaining element are 7. now lets consider the right subtree which have 3 nodes,here possibilities are 7C3 * 2. now we have 4 keys in our hand,out of that the smallest one will go to root of left subtree.remaining are 3 nodes. 3C1 for right child of left subtree of node. remaing 2 elements have only one choice as the largest among them will go to last level node & remaing is top of that. 7C3 * 2 * 3C1 = 210 mrinmoyh answered Jan 22, 2021 mrinmoyh comment Share Follow See all 3 Comments See all 3 3 Comments reply ayush.5 commented Jan 25, 2021 reply Follow Share @mrinmoyh , yes. $T(8)= 7C_{3}*$$T(4)*T(3)$ this means, after fixing root node, we can choose 3 out of 7 remaining nodes for right subtree and solve right subtree in T(3) and solve left subtree in T(4). This can be solved recursively to get 210 9 votes 9 votes kickassakash commented Jan 31, 2023 reply Follow Share @mrinmoyh can you please tell me how right subtree have possibility 7C3 *2 i get that it can be 7C3 but why multiply with 2 1 votes 1 votes prachnap123 commented Feb 3 reply Follow Share If there are n elements, then one of the element is always root depending upon minheap and maxheap.Rest (n-1) element will be the part of subtree.Divide these elements such that n1 + n2 = n-1, here n1 and n2 are the number of elements in left and right subtree.T(n) = (n-1)Cn1 * T(n1) * T(n2)..............Recursive function and T(1) = T(2) = 1 0 votes 0 votes Please log in or register to add a comment.