Question: Question 3 : Heaps . ( a ) Below are three different methods for building a max - heap. Suppose that A [ 1 ,
Question : Heaps
a Below are three different methods for building a maxheap. Suppose that A n
initially contains n elements, which are not necessarily in heap format. You may assume that the value of Aheapsize n For each method, justify whether the method correctly. builds a maxheap. If so justify your answer. If not, provide an example showing it doesnt work.
METHOD :
Initial call: MakeHeapA n MakeHeapA i
If i BubbleupAi
MakeHeapAi
METHOD :
Initial call: MakeHeapA n MakeHeapA i
If i BubbledownAi MakeHeapAi
METHOD :
Initial call: MakeHeapA MakeHeapA i
If i Aheapsize BubbleupAi
MakeHeapAi
b A minmaxheap is a complete binary tree containing alternating min and max levels. The root node is the minimum of all nodes in the tree. The nodes at the next level are the largest nodes of their subtrees. For each level after that, a node that is on a min level is the minimum of all nodes in its subtree, and a node that is on a max level, is the maximum of all nodes in its subtree. Below is an example of a minmax tree. Assume that the elements are stored in array A as for usual heaps, where Aheapsize indicates the size of the heap. Your job is write the pseudocode for a procedure that inserts into this type of heap. Call your procedure MinMaxInsertA k which inserts k into the heap, and carries out the necessary swaps in order to maintain the minmax heap property.
c Suppose A is a sorted list of exactly n elements, sorted in decreasing order. Suppose B a maxheap of exactly n elements. Two students are given the task of creating a sorted list of the combined elements from set A and B Below are their approaches:
Student : Add all elements of A and B to a new array C of size n Then uses bottomup heap building, resulting in a max heap in array C Next, run HeapSort on array C
Student : Repeat until A and B are empty: Compare first element of A with element B If top element of A is larger, extract it to the output list. If B is larger, call Deletemax, and store to the output list. If at some point B is empty, simply concatenate all remaining elements of A to the output list. If at some point A is empty, continue calling Deletemax on B until it is empty.
Determine the worstcase runtime for each method above. Is there one student with a more efficient approach?
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
