Question

An operator delete(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
Official answer and solution
Official answer
B
From
Official GATE 2016 answer key

Solution

Analyze the heap deletion algorithm by swapping with the last leaf and applying heapify.

In a binary heap of depth dd stored in an array, deleting the item at index ii is performed as follows:

  1. Replace the element at index ii with the last element of the heap (the rightmost leaf at the bottom level).
  2. Decrease the heap size by 1.
  1. The newly placed element may violate the heap ordering property in one of two directions:
    • If the element is smaller (in a max-heap) than its parent, it must bubble down using heapify-down (or sift-down). In the worst case, it traverses a path down to a leaf, taking at most O(d)O(d) comparisons and swaps.
    • If the element is larger (in a max-heap) than its parent, it must bubble up using heapify-up (or sift-up). In the worst case, it traverses a path up to the root, taking at most O(d)O(d) comparisons and swaps.

In either case, restoring the heap property takes at most O(d)O(d) time. Since it requires traversing tree levels and cannot be done in O(1)O(1) time in the worst case, the time complexity is O(d)O(d) but not O(1)O(1).

Final answer

B

Common trap

Confusing the number of tree levels dd with the number of nodes N=2d+1−1N = 2^{d+1} - 1, which would give O(2d)O(2^d).

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2016 Computer Science and Information Technology (CS) Session 1
Question
47 of 65, printed as CS Q.37
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.