Data Structures & AlgorithmsHardGATE CS · Infosys
The array [17, 12, 25, 28, 9, 16] (0-based indices) is turned into a binary min-heap by Floyd's method: for i from n/2 − 1 down to 0, sift the element at i down by repeatedly swapping it with its smaller child while that child is smaller. How many swaps are performed in total?
- A. 3
- B. 4
- C. 2
- D. 8
Correct answer: B. 4
Floyd's build-heap sifts from the last internal node back to the root. Each swap is counted once; building a max-heap, or sifting from the top down, performs a different number of swaps.
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
- The array [12, 25, 28, 11, 3, 5, 20, 23] (0-based indices) is turned into a binary min-heap by Floyd's method: for i from n/2 − 1 down to…
- A min-heap starts empty. The operations, in order, are: insert 17, insert 4, insert 12, delete-min, insert 8, insert 17, insert 19, insert…
- A min-heap starts empty. The operations, in order, are: insert 2, insert 20, insert 6, insert 15, insert 12, delete-min, insert 17, insert…
- A min-heap starts empty. The operations, in order, are: insert 13, insert 20, insert 11, insert 10, insert 3, insert 19, delete-min…
- A min-heap starts empty. The operations, in order, are: insert 2, insert 14, delete-min, delete-min, insert 15, insert 20, delete-min…
- A min-heap starts empty. The operations, in order, are: insert 7, delete-min, insert 14, insert 15, delete-min, insert 3, insert 3, insert…