Skip to content
Official PYQ · 2016

GATE CS 2016 Set 1 Q37 — Binary Heaps

Explain with AI

An operator delete(i)(i) for a binary heap data structure is to be designed to delete the item in the ii-th  node. Assume that the heap is implemented in an array and ii refers to the ii-th index of the array. If the heap tree has depth dd (number of edges on the path from the root to the farthest leaf ), then what is the time complexity to re-fix the heap efficiently after the removal of the element?

Choose one option
View worked solution

Answer: B

To delete an element at the ii-th index of a binary heap implemented as an array, we perform the following steps:

  1. Replace the element at index ii with the last element of the heap (at index nn).
  2. Reduce the heap size by 11.
  3. Restore the heap property (re-fix the heap) at index ii:
    • If the replaced element is larger than its parent (in a max-heap), we bubble it up towards the root (heapify-up). This takes at most O(d)O(d) time, where dd is the depth of the tree.
    • If the replaced element is smaller than its children, we bubble it down towards the leaves (heapify-down). This also takes at most O(d)O(d) time.

Thus, the overall time complexity to re-fix the heap is bounded by the depth of the tree, which is O(d)O(d).

Why it is not O(1)O(1):
In the worst case (for example, when we delete the root element), the replacement element must be bubbled down all the way to a leaf node. This requires traversing a path of length dd, taking Θ(d)\Theta(d) operations. Therefore, the time complexity is O(d)O(d) but not O(1)O(1).

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Binary Heaps questions