Statement for Linked Answer Questions 76 & 77:
A -ary max heap is like a binary max heap, but instead of children, nodes have children. A -ary heap can be represented by an array as follows: The root is stored in the first location, , nodes in the next level, from left to right, is stored from to . The nodes from the second level of the tree from left to right are stored from location onward. An item can be inserted into a -ary heap containing items by placing in the location 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 ary max heap?
77. Suppose the elements and are inserted, in that order, into the valid -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?
View worked solution
Answer: A
In a -ary heap represented by a -indexed array, the parent of a node at index is located at index .
From the previous question (Q.76), the valid -ary max heap is represented by the array:
We insert the elements and one by one:
- Insert :
- Place at the end: . Here, is at index .
- Its parent is at index , which has value .
- Since , swap them: .
- The parent of index is index (value ). Since , heap property is satisfied.
- Insert :
- Place at the end: . Here, is at index .
- Its parent is at index (value ). Since , no swap is needed.
- Insert :
- Place at the end: . Here, is at index .
- Its parent is at index (value ). Since , swap them: .
- Now, the parent of index is index (value ). Since , swap them: .
- Insert :
- Place at the end: . Here, is at index .
- Its parent is at index (value ). Since , no swap is needed.
The final array representation is: , which matches Option A.
Free practice · Your answers on this page are not saved.
