GATE / CS ARCHIVE

GATE CSE 2018 / QUESTION 46

Counting seven-key min-heaps

Data Structures · Heaps & Priority Queues · Full GATE CSE 2018 paper

GATE 2018, Q46 2 marks · NAT
Report issue

Heaps & Priority Queues

Question 46, GATE 2018: How many distinct min-heaps use each key 1,2,3,4,5,6,7 once? Use the standard complete binary heap shape.
Tap to enlarge
Answer & solution

Official answer: 80

Method

The shape is fixed: a root with two three-node subtrees. Minimum key 1 must occupy the root. Select a three-element subset of the remaining six keys for the left subtree; the right gets the complement, giving C(6,3)=20 distinct assignments. Within each subtree its least key must be its root, but its two leaves can be swapped independently. Left and right positions are distinguished in a heap, so do not divide the count by 2 for mirror images.

Solution

The minimum 1 must be the root. The complete seven-node shape has three positions in each subtree. Choose which three of the other six keys go left in C(6, 3)=20 ways. In either three-node subtree its smallest key is the root and its two leaves can be ordered in 2 ways. Thus there are 20*2*2=80 heaps.

Original paper, page 19 · Answer key