Home » Programming & Data Structure » Programming and data structure miscellaneous » Question

Programming and data structure miscellaneous

Programming & Data Structure

Direction: 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.

  1. Suppose the elements 7, 2, 10 and 4 are inserted in that order, into the valid 3 – ary max heap found in the above question 60. Which one of the following is the sequence of items in the array representing the resultant heap?
    1. 10, 7, 9, 8, 3, 1, 5, 2, 6, 4
    2. 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
    3. 10, 9, 4, 5, 7, 6, 8, 2, 1, 3
    4. 10, 8, 6, 9, 7, 2, 3, 4, 1, 5
Correct Option: A

Given heap is as follows

To add 7, 2, 10, 4 we add the node at the end of array

We keep if at right place in the heap tree. Compare elements with its parent node.
Since 10 > 6 and 7 > 5, we interchange.

Since 10 > 9, we interchange and we get

n/ 2 = 10/2 = 5
3 is at right position
4 is at right position
Order
10 7 9 8 3 1 5 2 6 4
Hence (a) is correct option.



Your comments will be displayed only after manual approval.