GATE / CS ARCHIVE

GATE CSE 2019 / QUESTION 40

Heap ordering and conversion complexity

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

GATE 2019, Q40 2 marks · MCQ
Report issue

Heaps & Priority Queues

Question 40, GATE 2019: Test max-heap statements: I the smallest element is always at a leaf; II the second largest is always a root child; III a max-heap can be built from a BST in Θ(n); IV a BST can be built from a max-heap in Θ(n). Options A I/II/III, B I/II/IV, C I/III/IV, D II/III/IV.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Our answer: A under distinct-key comparison model

1. Under the intended distinct-key model, a max-heap parent is larger than its children. If the smallest key were internal, one of its children would be smaller still, a contradiction. Hence I holds.
2. The largest key is at the root. If the second largest were below depth one, its nonroot parent would have to be larger, producing a third key above it in rank. Thus it must be one of the root's children and II holds.
3. To establish III, traverse the BST once and copy its n keys into an array, costing Θ(n). Then run bottom-up heap construction, also Θ(n), by sifting down internal nodes. Sorting first is not required.
4. IV is false in the general comparison model. If it were possible, arbitrary distinct keys could be heapified in Θ(n), converted to a BST in Θ(n), and output in sorted order by an inorder traversal in Θ(n). That would contradict the Ω(n log n) comparison-sorting lower bound.
5. Therefore the intended true statements are I, II, III: option A. The qualification about distinct keys is material. With repeated minimum values, a minimum can occur at an internal node as well as a leaf, so the literal word 'always' in I is not valid for unrestricted duplicate-key heaps. Specialized bounded-integer models also cannot be silently substituted for comparison complexity.

Original paper, page 13 · Answer key