Data Structures & AlgorithmsVery hardGATE CS · Codeforces Div2 B

Distinct integers are inserted in this order into an empty binary min-heap stored in a 0-indexed array (children of index i at 2i+1 and 2i+2; sift-up on insertion): 4 43 20 57 94 13 93 37 64 68 14 79 47 86. Then the minimum is removed by the standard method: the last array element moves to the root and is sifted down, swapping with the smaller child while it is larger than that child. What is the array afterwards?

  1. A. 13 14 20 43 37 47 86 57 64 94 68 79 93
  2. B. 14 13 43 37 20 86 57 64 94 68 79 47 93
  3. C. 93 14 13 43 37 20 86 57 64 94 68 79 47
  4. D. 14 43 13 64 37 20 86 57 93 94 68 79 47

Correct answer: A. 13 14 20 43 37 47 86 57 64 94 68 79 93

The minimum is the root; after removal, the moved element must sink past the SMALLER child to keep the heap valid. Sifting past the larger child, shifting the whole array, or not sifting at all are the traps.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic