Skip to content
Official PYQ · 2006

GATE CS 2006 Q77 — Binary Heaps

Explain with AI

Statement for Linked Answer Questions 76 & 77:

A 33-ary max heap is like a binary max heap, but instead of 22 children, nodes have 33 children. A 33-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 33-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,91,3,5,6,8,9
  2. 9,6,3,1,8,59,6,3,1,8,5
  3. 9,3,6,8,5,19,3,6,8,5,1
  4. 9,5,6,8,3,19,5,6,8,3,1


77. Suppose the elements 7,2,107, 2, 10 and 44 are inserted, in that order, into the valid 33-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?

Choose one option
View worked solution

Answer: A

In a 33-ary heap represented by a 00-indexed array, the parent of a node at index ii is located at index ⌊(i−1)/3⌋\lfloor (i-1)/3 \rfloor.

From the previous question (Q.76), the valid 33-ary max heap is represented by the array:
a=[9,5,6,8,3,1]a = [9, 5, 6, 8, 3, 1]

We insert the elements 7,2,10,7, 2, 10, and 44 one by one:

  1. Insert 77:
    • Place 77 at the end: [9,5,6,8,3,1,7][9, 5, 6, 8, 3, 1, \mathbf{7}]. Here, 77 is at index 66.
    • Its parent is at index ⌊(6−1)/3⌋=1\lfloor (6-1)/3 \rfloor = 1, which has value 55.
    • Since 7>57 > 5, swap them: [9,7,6,8,3,1,5][9, \mathbf{7}, 6, 8, 3, 1, \mathbf{5}].
    • The parent of index 11 is index 00 (value 99). Since 9≥79 \ge 7, heap property is satisfied.
  2. Insert 22:
    • Place 22 at the end: [9,7,6,8,3,1,5,2][9, 7, 6, 8, 3, 1, 5, \mathbf{2}]. Here, 22 is at index 77.
    • Its parent is at index ⌊(7−1)/3⌋=2\lfloor (7-1)/3 \rfloor = 2 (value 66). Since 2≤62 \le 6, no swap is needed.
  3. Insert 1010:
    • Place 1010 at the end: [9,7,6,8,3,1,5,2,10][9, 7, 6, 8, 3, 1, 5, 2, \mathbf{10}]. Here, 1010 is at index 88.
    • Its parent is at index ⌊(8−1)/3⌋=2\lfloor (8-1)/3 \rfloor = 2 (value 66). Since 10>610 > 6, swap them: [9,7,10,8,3,1,5,2,6][9, 7, \mathbf{10}, 8, 3, 1, 5, 2, \mathbf{6}].
    • Now, the parent of index 22 is index 00 (value 99). Since 10>910 > 9, swap them: [10,7,9,8,3,1,5,2,6][\mathbf{10}, 7, \mathbf{9}, 8, 3, 1, 5, 2, 6].
  4. Insert 44:
    • Place 44 at the end: [10,7,9,8,3,1,5,2,6,4][10, 7, 9, 8, 3, 1, 5, 2, 6, \mathbf{4}]. Here, 44 is at index 99.
    • Its parent is at index ⌊(9−1)/3⌋=2\lfloor (9-1)/3 \rfloor = 2 (value 99). Since 4≤94 \le 9, no swap is needed.

The final array representation is: [10,7,9,8,3,1,5,2,6,4][10, 7, 9, 8, 3, 1, 5, 2, 6, 4], which matches Option A.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Binary Heaps questions