Skip to main content

GATE CSE 2006Data Structures › Heaps & Priority QueuesVerified

GATE CSE 2006 PYQ: Statement for Linked Answer Questions 76 & 77: A 3 3 3 -ary max heap is like a binary max heap, but instead…

Statement for Linked Answer Questions 76 & 77:

A 3-ary max heap is like a binary max heap, but instead of 2 children, nodes have 3 children. A 3-ary heap can be represented by an array as follows: The root is stored in the first location, a[0]a[0], nodes in the next level, from left to right, is stored from a[1]a[1] to a[3]a[3]. The nodes from the second level of the tree from left to right are stored from a[4]a[4] location onward. An item xx can be inserted into a 3-ary heap containing nn items by placing xx in the location a[n]a[n] and pushing it up the tree to satisfy the heap property.

76. Which one of the following is a valid sequence of elements in an array representing 3−3-ary max heap?

  1. 1,3,5,6,8,9
  2. 9,6,3,1,8,5
  3. 9,3,6,8,5,1
  4. 9,5,6,8,3,1


77. Suppose the elements 7,2,107, 2, 10 and 4 are inserted, in that order, into the valid 3-ary max heap found in the previous question, Q.76. Which one of the following is the sequence of items in the array representing the resultant heap?

  1. A
    1,3,5,6,8,9
    Correct answer
  2. B
    9,6,3,1,8,5
  3. C
    9,3,6,8,5,1
  4. D
    9,5,6,8,3,1
    Correct answer

Answer

(A, D) 1 , 3 , 5 , 6 , 8 , 9 1,3,5,6,8,9 1 , 3 , 5 , 6 , 8 , 9; 9 , 5 , 6 , 8 , 3 , 1 9,5,6,8,3,1 9 , 5 , 6 , 8 , 3 , 1

Approach

Use the array-to-tree mapping of a 3-ary heap: the node at index i has children at indices 3i+1, 3i+2, 3i+3, and its parent at floor((i-1)/3). For Q76, accept only the array in which every parent is at least as large as each of its (existing) children. For Q77, simulate the standard insert-by-sift-up procedure (place the new key at a[n], then repeatedly swap with its parent while it exceeds the parent) starting from that valid heap.

Only the array 9,5,6,8,3,1 (Q76 option D) satisfies the 3-ary max-heap condition under the indexing i -> {3i+1, 3i+2, 3i+3}. Inserting 7, 2, 10, 4 in order into that heap with the sift-up procedure yields 10,7,9,8,3,1,5,2,6,4, which is Q77 option A. Both the official-style answer and the dominant community answer agree with this.

Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.