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 (the children of index i are at 2i+1 and 2i+2, and a new element is sifted up while it is smaller than its parent): 52 62 25 94 12 47 79 15 98 68 82 28 45 99. What is the array after all insertions?
- A. 52 62 25 94 12 47 79 15 98 68 82 28 45 99
- B. 99 94 98 62 82 45 79 15 52 12 68 25 28 47
- C. 12 15 28 25 62 45 79 94 98 68 82 52 47 99
- D. 12 15 28 25 62 47 45 79 98 68 82 52 94 99
Correct answer: C. 12 15 28 25 62 45 79 94 98 68 82 52 47 99
Every insertion appends to the end and swaps upward past larger parents; the parent of index i is (i-1)//2. Skipping the sift-up, building a max-heap, or using i//2 as the parent gives the trap arrays.
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 (the children of index i are at…
- Distinct integers are inserted in this order into an empty binary min-heap stored in a 0-indexed array (the children of index i are at…
- Distinct integers are inserted in this order into an empty binary min-heap stored in a 0-indexed array (the children of index i are at…
- Distinct integers are inserted in this order into an empty binary min-heap stored in a 0-indexed array (the children of index i are at…
- Distinct integers are inserted in this order into an empty binary min-heap stored in a 0-indexed array (the children of index i are at…
- 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…