GATE CS 2006 · Arrays & Matrix Representation

MCQ · 1 mark · Problem PROG-DSTR-0208

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], nodes in the next level, from left to right, is stored from a[1] to a[3]. The nodes from the second level of the tree from left to right are stored from a[4] location onward. An item x can be inserted into a 3-ary heap containing n items by placing x in the location a[n] and pushing it up the tree to satisfy the heap property.Suppose the elements 7, 2, 10 and 4 are inserted, in that order, into the 3-ary max heap stored as the array 9, 5, 6, 8, 3, 1. Which one of the following is the sequence of items in the array representing the resultant heap?
  1. A
    10, 7, 9, 8, 3, 1, 5, 2, 6, 4
  2. B
    10, 9, 8, 7, 6, 5, 4, 3, 2, 1
  3. C
    10, 9, 4, 5, 7, 6, 8, 2, 1, 3
  4. D
    10, 8, 6, 9, 7, 2, 3, 4, 1, 5

Solve it on GATE DA practice →More Basic Data Structures problems

Show the worked solution\[\boxed{\text{In a 3-ary heap stored from } a[0], \text{ the parent of } a[i] \text{ is } a[\lfloor (i-1)/3\rfloor]}\]Start: \(9, 5, 6, 8, 3, 1\).Insert \(7\) at \(a[6]\): parent \(a[1]=5\) is smaller, swap; parent \(a[0]=9\) is larger, stop: \(9, 7, 6, 8, 3, 1, 5\).Insert \(2\) at \(a[7]\): parent \(a[2]=6\) is larger, stop: \(9, 7, 6, 8, 3, 1, 5, 2\).Insert \(10\) at \(a[8]\): swap with \(a[2]=6\), then with \(a[0]=9\): \(10, 7, 9, 8, 3, 1, 5, 2, 6\).Insert \(4\) at \(a[9]\): parent \(a[3]=8\) is larger, stop: \(10, 7, 9, 8, 3, 1, 5, 2, 6, 4\).Answer: A

GATE DA practice: past exam problems by chapter, with worked solutions · gatedaprep.com