GATE / CS ARCHIVE

GATE CSE 2008 / QUESTION 47

Batch insertion into a binary heap

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

GATE 2008, Q47 2 marks · MCQ
Report issue

Heaps & Priority Queues

Question 47, GATE 2008: A binary heap contains n elements. Insert another n elements, not necessarily one at a time. What is the optimum total time?

(A) Θ(log n)
(B) Θ(n)
(C) Θ(n log n)
(D) Θ(n²)
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

1. Append all n new elements to the existing n-element heap array, then run bottom-up BUILD-HEAP on the resulting 2n elements. Its total sift-down work is O(n), since the number of nodes of height h decreases geometrically and Σh/2h is constant.
2. Reading/storing n new elements requires Ω(n), so the optimum total is Θ(n), option B. The permission to insert them in a batch is crucial: n separate ordinary heap insertions can require Θ(n log n).

Original paper, page 8