GATE / CS ARCHIVE

SIMILAR QUESTIONS / DATA STRUCTURES

Simulate successive heap insertions

5 questions solved the same way, newest first. Back to Data Structures

How these are solved

Append each key at the next complete-tree position and swap upward while parent order is violated; repeat for all inserted keys. 2006 uses ternary parent indices; other members binary.

2 marks · MCQ
Report issue

Heaps & Priority Queues

Question 37, GATE 2015: Insert 35 into the max-heap array (indices 1–9) [40,30,20,10,15,16,17,8,4]. A [40,30,20,10,15,16,17,8,4,35]; B [40,35,20,10,30,16,17,8,4,15]; C [40,30,20,10,35,16,17,8,4,15]; D [40,35,20,10,15,16,17,8,4,30].
Tap to enlarge
Your answer
Answer & solution

Official answer: B

Step through it (5 steps)

Preserve the complete-tree shape by inserting at the next free index 10, then repair only its ancestor path. Its parent is floor(10/2)=5, holding 15, so swap. At index 5 the parent is 2, holding 30, so swap again. At index 2 the parent is 1, holding 40, so stop. Option C stops too early and violates 30≥35.

Solution: Append35 at index 10. It exceeds its parent15 at index 5, then 30 at index 2, but not 40 at the root. The array becomes 40, 35, 20, 10, 30, 16, 17, 8, 4, 15, option B.

Original paper, page 14 · Answer key

1 mark · MCQ
Report issue

Asked again GATE 2005, Q34 (same question)

Heaps & Priority Queues

Question 12, GATE 2014: A max-heap initially has level order 10,8,5,3,2. Insert 1 and then 7. What is the new level order? A 10,8,7,3,2,1,5; B 10,8,7,2,3,1,5; C 10,8,7,1,2,3,5; D 10,8,7,5,3,2,1.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Step through it (5 steps)

1. Set up the reasoning. Represent the heap as a one-based array. Append each item at the first available position to preserve the complete-tree shape, then compare it only with its ancestors while restoring max-heap order.

2. Derive the result. Appending1 puts it below 5 and requires no swap. Appending7 puts it below 5, so exchange 7 and 5; 7 is below root10 and stops. The resulting level order is 10, 8, 7, 3, 2, 1, 5, A.

3. Check the interpretation. Heap insertion does not sort an entire level. Inserting 7 affects the path through its parent 5; it does not exchange the existing siblings 3 and 2. This distinguishes A from other arrays that might still satisfy some heap inequalities.

Original paper, page 32 · Answer key

2 marks · MCQ
Report issue

Four insertions into ternary heap

Heaps & Priority Queues

Question 77, GATE 2006: A ternary max-heap is stored level-by-level, left-to-right, in a zero-based array. Root is a[0]; children of i are 3i+1,3i+2,3i+3. Insert by appending then moving upward until the parent is at least as large. Start from the valid heap [9,5,6,8,3,1] and insert 7,2,10,4 in that order. What final array results?
Options:
(A) [10,7,9,8,3,1,5,2,6,4]
(B) [10,9,8,7,6,5,4,3,2,1]
(C) [10,9,4,5,7,6,8,2,1,3]
(D) [10,8,6,9,7,2,3,4,1,5]
Question 77, GATE 2006: A ternary max-heap is stored level-by-level, left-to-right, in a zero-based array. Root is a[0]; children of i are 3i+1,3i+2,3i+3. Insert by appending then moving upward until the parent is at least as large. Start from the valid heap [9,5,6,8,3,1] and insert 7,2,10,4 in that order. What final array results?
Options:
(A) [10,7,9,8,3,1,5,2,6,4]
(B) [10,9,8,7,6,5,4,3,2,1]
(C) [10,9,4,5,7,6,8,2,1,3]
(D) [10,8,6,9,7,2,3,4,1,5]
Question 77, GATE 2006: A ternary max-heap is stored level-by-level, left-to-right, in a zero-based array. Root is a[0]; children of i are 3i+1,3i+2,3i+3. Insert by appending then moving upward until the parent is at least as large. Start from the valid heap [9,5,6,8,3,1] and insert 7,2,10,4 in that order. What final array results?
Options:
(A) [10,7,9,8,3,1,5,2,6,4]
(B) [10,9,8,7,6,5,4,3,2,1]
(C) [10,9,4,5,7,6,8,2,1,3]
(D) [10,8,6,9,7,2,3,4,1,5]
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

Step through it (9 steps)

1. Start with [9,5,6,8,3,1]. Insert 7 at index 6 and exchange with parent index 1, obtaining [9,7,6,8,3,1,5].

2. Insert 2 at index 7, whose parent 6 is already larger: [9,7,6,8,3,1,5,2]. Insert 10 at index 8, exchange with parent index 2 (6), then root 9, obtaining [10,7,9,8,3,1,5,2,6].

3. Insert 4 at index 9 under parent 9, with no exchange. Final array [10,7,9,8,3,1,5,2,6,4], A.

4. Teaching check: The parent index is floor((i−1)/3). After appending 10 at index 8, its parent is index 2, then the root, so two upward swaps occur. Appending 4 at index 9 places it under the current 9 and requires no swap.

Original paper, page 12

2 marks · MCQ
Report issue

Asked again GATE 2014 Set 2, Q12 (same question)

Inserting into a max heap

Heaps & Priority Queues

Question 34, GATE 2005: Max-heap level order 10, 8, 5, 3, 2 receives 1 then 7. New level order? A10, 8, 7, 5, 3, 2, 1; B10, 8, 7, 2, 3, 1, 5; C10, 8, 7, 1, 2, 3, 5; D10, 8, 7, 3, 2, 1, 5.
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

Step through it (5 steps)

Append 1 as the sixth element; its parent 5 is larger, so no swap occurs. Append 7 as the seventh element; swap it with parent 5, but stop below root 10. The resulting level order is 10, 8, 7, 3, 2, 1, 5, D.

Appending 1 puts it under parent 5 and requires no upward swap. Appending 7 puts it as the other child of 5;7 exceeds 5, so swap them. Its new parent is 10, which is larger, ending the repair. Other nodes remain where they were, yielding 10, 8, 7, 3, 2, 1, 5. A heap is not a sorted array; sorting the level order would unnecessarily rearrange nodes.

Original paper, page 4

2 marks · MCQ
Report issue

Heap built by successive insertions

Heaps & Priority Queues

Question 37, GATE 2004: Insert 32,15,20,30,12,25,16 one at a time into an empty binary max-heap, using upward restoration after each insertion. Which resulting heap is obtained? The four source diagrams are encoded below as level-order arrays (root first, left-to-right at each level).
Options:
(A) [32,30,25,15,12,20,16]
(B) [32,25,30,12,15,20,16]
(C) [32,30,25,15,12,16,20]
(D) [32,25,30,12,15,16,20]
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

Step through it (11 steps)

1. Insert32,15,20, then 30 bubbles above 15, giving[32,30,20,15]. Insert12 unchanged.

2. Insert25 bubbles above 20, giving[32,30,25,15,12,20];16 remains under 25. Final level order is 32,30,25,15,12,20,16, diagram A.

3. Teaching check: Insertion order matters: the final heap need not be the one obtained by bottom-up heapifying the array or sorting its values. Inserting 30 swaps it above 15; inserting 25 swaps it above 20. All remaining parent-child inequalities already hold.

Original paper, page 5