A 3-ary max heap is like a binary max heap, but instead of 2 children, nodes…

2006

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. Which one of the following is a valid sequence of elements in an array representing 3-ary max heap?

  1. A.

    1, 3, 5, 6, 8, 9

  2. B.

    9, 6, 3, 1, 8, 5

  3. C.

    9, 3, 6, 8, 5, 1

  4. D.

    9, 5, 6, 8, 3, 1

Attempted by 107 students.

Show answer & explanation

Correct answer: D

Key idea: For a 3-ary max heap stored in array a[], the children of the node at index i (0-based) are at indices 3i+1, 3i+2, and 3i+3. The max-heap property requires each parent value to be greater than or equal to each of its children.

We check the candidate sequence: 9, 5, 6, 8, 3, 1

  • Index 0 (value 9): children at indices 1, 2, 3 with values 5, 6, 8. Each child (5, 6, 8) is less than or equal to 9, so this node satisfies the max-heap property.

  • Index 1 (value 5): children at indices 4 and 5 with values 3 and 1. Both 3 and 1 are less than or equal to 5, so this node satisfies the max-heap property.

  • Index 2 (value 6): its children would be at indices 7, 8, 9, but those indices are out of bounds for this array, so there are no children to check.

Conclusion: Every parent in the array is greater than or equal to its children, so the sequence 9, 5, 6, 8, 3, 1 is a valid representation of a 3-ary max heap.

Explore the full course: Gate Guidance By Sanchit Sir