Heaps & Priority Queues


Shared context
Insert the specified sequence into an initially empty min-heap and draw the final heap and its state after deleting the root. Both are solved separately below; no official original is available to adjudicate.
Answer & solution
Answer: [1,4,2,7,5,6,3] → [2,4,3,7,5,6] (no official key was published)
A binary min-heap has complete-tree shape and each parent no larger than either child. Store it in level order. On insertion append at the next free position and swap upward while smaller than its parent. Do not sort the whole array: a heap is only partially ordered.
[7]
[6, 7]
[5, 7, 6]
[4, 5, 6, 7]
[3, 4, 6, 7, 5]
[2, 4, 3, 7, 5, 6]
[1, 4, 2, 7, 5, 6, 3]
The resulting tree is:
1
/ \
4 2
/ \ / \
7 5 6 3Delete the root by moving the final element 3 into its position and removing that final slot. This gives [3,4,2,7,5,6]. Swap 3 with its smaller child 2, giving [2,4,3,7,5,6]. Now 3≤6, so downward repair stops. The final tree is:
2
/ \
4 3
/ \ /
7 5 6The paper instead orders the last three insertions as 2,3,1. Its successive arrays are:
[7]
[6, 7]
[5, 7, 6]
[4, 5, 6, 7]
[2, 4, 6, 7, 5]
[2, 4, 3, 7, 5, 6]
[1, 4, 2, 7, 5, 6, 3]
Its root-deletion result therefore also coincides. Preserving both source sequences is still important: agreement of the final answer does not establish that the printed questions were identical.