GATE / CS ARCHIVE

GATE CSE 1999 / QUESTION 12(b)

Min-heap insertion and root deletion

Data Structures · Heaps & Priority Queues · Full GATE CSE 1999 paper

GATE 1999, Q12(b) 2 marks · Descriptive
Report issue

Heaps & Priority Queues

Question 12(b), GATE 1999: Insert the specified sequence into an initially empty min-heap and draw the final heap and its state after deleting the root. Source discrepancy: GateForum p11 gives 7,6,5,4,3,2,1; GFG p7 visibly gives 7,6,5,4,2,3,1. Both are solved separately below; no official original is available to adjudicate.
Question 12(b), GATE 1999: Insert the specified sequence into an initially empty min-heap and draw the final heap and its state after deleting the root. Source discrepancy: GateForum p11 gives 7,6,5,4,3,2,1; GFG p7 visibly gives 7,6,5,4,2,3,1. Both are solved separately below; no official original is available to adjudicate.
Tap to enlarge
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   3

Delete 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   6

The 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.

Original paper, page 7