Data Structures & AlgorithmsHardGATE 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): 47 28 2 58 71 90 66 37 39 31 51. 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?
- A. 31 28 39 37 90 66 58 47 71 51
- B. 28 31 51 39 37 90 66 58 47 71
- C. 51 31 28 39 37 90 66 58 47 71
- D. 31 39 28 51 37 90 66 58 47 71
Correct answer: B. 28 31 51 39 37 90 66 58 47 71
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.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- 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…
- 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…
- 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…
- 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…
- 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…
- 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…