GATE / CS ARCHIVE

SIMILAR QUESTIONS / DATA STRUCTURES

Check heap shape and parent order

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

How these are solved

Verify complete level-order shape and every parent-child max/min inequality using the stated arity/index convention. 1991 identifies a minheap;

1 mark · MCQ
Report issue

Heaps & Priority Queues

Question 12, GATE 2023: Which 1-indexed array is a max-heap? A [23,17,10,6,13,14,1,5,7,12]; B [23,17,14,7,13,10,1,5,6,12]; C [23,17,14,6,13,10,1,5,7,15]; D [23,14,17,1,10,13,16,12,7,5].
Tap to enlarge
Your answer
Answer & solution

Official answer: B

In a one-indexed max-heap, A[i] must be at least both A[2i] and A[2i+1] when those children exist. B satisfies all parent-child inequalities: 23≥17, 14; 17≥7, 13; 14≥10, 1; 7≥5, 6; 13≥12. A fails at 10<14, C at 6<7 and 13<15, and D at 1<12. Answer B.

Teaching detail. A heap is only partially ordered: parent keys dominate child keys, but siblings need not be sorted and the array need not be globally descending. There are only five possible parent positions in this ten-element array. For A, index 3 contains 10 and child index 6 contains 14. For C, parent index 4 contains 6 but child 9 contains 7. For D, parent 4 contains 1 but child 8 contains 12. One violation suffices to reject a candidate; every edge must pass to accept B.

Original paper, page 9 · Answer key

1 mark · MCQ
Report issue

Recognizing a max-heap

Heaps & Priority Queues

Question 23, GATE 2011: Which drawn binary tree is a max-heap? Below each node is specified as value:(left child,right child), with '-' for a missing child.

(A) 10:(8,6), 8:(4,-), 6:(5,2), 5:(1,-); remaining nodes are leaves
(B) 10:(8,6), 8:(4,5), 6:(1,2); remaining nodes are leaves
(C) 10:(5,6), 5:(4,8), 6:(2,1); remaining nodes are leaves
(D) 5:(2,8), 2:(1,4), 8:(6,10); remaining nodes are leaves
Tap to enlarge
Your answer
Answer & solution

Official answer: B

A binary max-heap must have complete-tree shape as well as parent≥child values. B is complete and satisfies 10≥8, 6; 8≥4, 5; 6≥1, 2. A satisfies value order but not completeness. C has child8 greater than parent5; D also violates value order. Thus B.

Completeness requires every level except possibly the last to be full and the last level to be filled from left to right. In A, node 8 lacks a right child while positions to its right are occupied, and a deeper node exists despite that gap. In B every displayed level is full, and every parent dominates its two children. C fails at 5→8; D fails already at 5→8 and also at 2→4 and 8→10. Checking only the root maximum is insufficient.

Original paper, page 6 · Answer key

2 marks · MCQ
Report issue

Recognize an array max-heap

Heaps & Priority Queues

Question 59, GATE 2009: Which zero-based array is a binary max-heap? A[25, 12, 16, 13, 10, 8, 14]; B[25, 14, 13, 16, 10, 8, 12]; C[25, 14, 16, 13, 10, 8, 12]; D[25, 14, 12, 13, 10, 8, 16].
Question 59, GATE 2009: Which zero-based array is a binary max-heap? A[25, 12, 16, 13, 10, 8, 14]; B[25, 14, 13, 16, 10, 8, 12]; C[25, 14, 16, 13, 10, 8, 12]; D[25, 14, 12, 13, 10, 8, 16].
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

In a zero-based heap each parent at i must be at least as large as children at 2i+1 and 2i+2. In C, [25, 14, 16, 13, 10, 8, 12], the checks are 25≥14, 16; 14≥13, 10; and 16≥8, 12, so every edge satisfies max-heap order. A fails because parent 12 has child 13; B fails because parent 14 has child 16; D fails because parent 12 has child 16. Thus C alone is a max-heap.

A max-heap needs parent≥child along every edge, not a globally descending array. In C, root 25 exceeds 14/16; 14 exceeds 13/10; 16 exceeds 8/12. A fails at 12<13; B at 14<16; D at 12<16. Checking only the root would wrongly admit the distractors; every internal node must satisfy the local condition.

Original paper, page 14

2 marks · MCQ
Report issue

Valid array representation of ternary max-heap

Heaps & Priority Queues

Question 76, 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. Which array is a valid heap?
Options:
(A) [1,3,5,6,8,9]
(B) [9,6,3,1,8,5]
(C) [9,3,6,8,5,1]
(D) [9,5,6,8,3,1]
Question 76, 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. Which array is a valid heap?
Options:
(A) [1,3,5,6,8,9]
(B) [9,6,3,1,8,5]
(C) [9,3,6,8,5,1]
(D) [9,5,6,8,3,1]
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

1. In a zero-based ternary heap, children of i are 3i+1,3i+2,3i+3. For D=[9,5,6,8,3,1], root 9 dominates 5,6,8 and node 5 dominates children 3,1.

2. A fails at the root; B has parent 6 with child 8; C has parent 3 with child 5. Only D is a valid max-heap.

3. Teaching check: Heap order is a parent-child condition, not full sorting. For six entries only root 0 and node 1 have children; checking these is sufficient. Option D’s 8 may exceed node 1’s 5 because they are siblings, so that comparison imposes no restriction.

Original paper, page 12

1 mark · Descriptive
Report issue

Identifying the tree structure

Heaps & Priority Queues

Question 14(a), GATE 1991: The complete binary tree has level-order array [1,5,18,7,13,25,20,11,9,15,17,27]. Its edges are 1→(5,18), 5→(7,13), 18→(25,20), 7→(11,9), 13→(15,17), and 25→(27,left only). Identify the data structure.
Question 14(a), GATE 1991: The complete binary tree has level-order array [1,5,18,7,13,25,20,11,9,15,17,27]. Its edges are 1→(5,18), 5→(7,13), 18→(25,20), 7→(11,9), 13→(15,17), and 25→(27,left only). Identify the data structure.
Question 14(a), GATE 1991: The complete binary tree has level-order array [1,5,18,7,13,25,20,11,9,15,17,27]. Its edges are 1→(5,18), 5→(7,13), 18→(25,20), 7→(11,9), 13→(15,17), and 25→(27,left only). Identify the data structure.
Tap to enlarge
Answer & solution

Answer: A binary min-heap. (no official key was published)

Two separate conditions identify this tree. First, its shape is complete: all levels above the last are full and the last level fills from left to right. Second, every parent is no greater than either child: for example 1≤5,18; 5≤7,13; 18≤25,20; and 7≤11,9. The minimum element is therefore at the root, and the tree is a binary min-heap.

It is not a binary search tree. For instance, the root's left subtree contains 5 even though 5>1, and the left child 25 of node 18 exceeds its parent. A heap imposes parent-child order but imposes no sorted order between siblings or across unrelated subtrees. The level-order array in the prompt fully specifies the source figure and provides the customary compact heap representation.

Original paper, page 8