GATE / CS ARCHIVE

GATE CSE 1991 / QUESTION 14(c)

Deleting any min-heap node

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

GATE 1991, Q14(c) 3 marks · Descriptive
Report issue

Heaps & Priority Queues

Question 14(c), GATE 1991: The complete binary tree has level-order array [1,5,18,7,13,25,20,11,9,15,17,27]. Its edges are 1→(5,18), 5→(7,13), 18→(25,20), 7→(11,9), 13→(15,17), and 25→(27,left only). Give pseudocode to delete an arbitrary node of an n-node min-heap, with worst-case complexity. Distinguish a supplied node index from a supplied key.
Question 14(c), GATE 1991: The complete binary tree has level-order array [1,5,18,7,13,25,20,11,9,15,17,27]. Its edges are 1→(5,18), 5→(7,13), 18→(25,20), 7→(11,9), 13→(15,17), and 25→(27,left only). Give pseudocode to delete an arbitrary node of an n-node min-heap, with worst-case complexity. Distinguish a supplied node index from a supplied key.
Question 14(c), GATE 1991: The complete binary tree has level-order array [1,5,18,7,13,25,20,11,9,15,17,27]. Its edges are 1→(5,18), 5→(7,13), 18→(25,20), 7→(11,9), 13→(15,17), and 25→(27,left only). Give pseudocode to delete an arbitrary node of an n-node min-heap, with worst-case complexity. Distinguish a supplied node index from a supplied key.
Tap to enlarge
Answer & solution

Answer: Replace the node by the last key and sift up or down as needed. Known-index deletion costs O(log n); locating an arbitrary key can add Θ(n). (no official key was published)

Assume a one-based array H[1..n] and a valid index p. Replacing H[p] by H[n] and shortening the array preserves completeness. The replacement may be smaller than its parent or larger than a child, so a general deletion cannot always use only sift-down.

DELETE_AT(H,n,p):
  require 1 <= p <= n
  if p == n: return n-1
  H[p] := H[n]
  n := n-1
  if p > 1 and H[p] < H[floor(p/2)]:
    while p > 1 and H[p] < H[floor(p/2)]:
      swap H[p], H[floor(p/2)]
      p := floor(p/2)
  else:
    while 2*p <= n:
      c := 2*p
      if c+1 <= n and H[c+1] < H[c]: c := c+1
      if H[p] <= H[c]: break
      swap H[p], H[c]
      p := c
  return n

Only inequalities incident to the replaced node can initially fail. If the replacement is less than its parent, it is also less than the old children, since those children were at least the old value, which was at least the parent. Thus only upward repair is needed. Otherwise the parent relation holds, and downward repair with the smaller child fixes each level without creating a violation above. Equal values need no swap.

A complete binary tree has height floor(log2 n). Either repair traverses at most that many levels and uses O(1) extra space, so a supplied-index deletion is O(log n) worst case. If only a key is supplied and no key-to-index directory exists, a heap is not a search tree: finding the occurrence can cost Θ(n), making the entire deletion Θ(n) worst case. A maintained locator can remove that search cost but must be updated on swaps and handle duplicates explicitly.

Original paper, page 8