GATE / CS ARCHIVE

SIMILAR QUESTIONS / DATA STRUCTURES

Repair a heap after arbitrary-node deletion

2 questions solved the same way, newest first. Back to Data Structures

How these are solved

Once the target node has been located, replace it by the last complete-tree element and sift up or down to restore heap order.

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

3 marks · Descriptive
Report issue

Deleting any min-heap node

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