GATE / CS ARCHIVE

GATE CSE 2016 SET 1 / QUESTION 37

GATE CSE 2016 Set 1 Q37

Data Structures · Heaps & Priority Queues · Full GATE CSE 2016 Set 1 paper

GATE 2016 Set 1, Q37 2 marks · MCQ
Report issue

Heaps & Priority Queues

Question 37, GATE 2016: An array-stored binary heap has depth d, measured in root-to-farthest-leaf edges. After deleting the item at arbitrary index i, what is the efficient worst-case time for restoring heap order? A: O(1). B: O(d) but not O(1). C: O(2^d) but not O(d). D: O(d·2^d) but not O(2^d).
Tap to enlarge
Your answer
Answer & solution

Official answer: B

Replace the deleted element with the last heap element, reduce the heap size, then sift up if the parent order is violated or otherwise sift down. At most one root-to-leaf path is traversed, length≤d. Some deletions require movement throughΘ(d) levels, so the worst-case bound is O(d) and not O(1), B.

Replacing the deleted slot with the last array element preserves the complete-tree shape and avoids shifting an entire subtree. For a min-heap, compare the replacement with its parent: if it is too small, move it upward; otherwise any violation can only be below it, so move it downward by choosing the smaller child. The max-heap case reverses inequalities. There are at most d edges on the chosen path, with constant work at each, and worst-case inputs require traversal to the root or deepest leaf.

Original paper, page 15 · Answer key