0

I got this question in an exam, but I'm not sure that I understand what it wants me to do. Can you please clarify that if I did the correct thing?

Question

An integer array A is passed to the function makeHeap. if A[0] contains n, then A[1] to A[n-1] contain numbers in arbitrary order. Write makeHeap such that A[1] to A[n-1] contain a min-heap. Your function must create the heap by processing the elements in the order A[2] , A[3] ,... , A[n-1].

My solution

void makeHeap(int A[], int n)
{
  int r = n -1 ; 
  for(int i = 1; i <= n/2; i++)
    siftUp(a, i , r );
}

/* i- parent , r - right of the array */ 
void siftUp(int A[], int i , int r )
{
   boolean done = false ; 
   int j = 2* i ; 
   while (j <= r && !done)
   {
     if(A[j] > A[j+1]) // find the smalest child
       j+=1 ; 
     if(A[i] > A[j])
     {
       // code to swap A[i] and A[j]
       i = j ; 
       j = i*2 ;

     } 
     else done = true ; 
   }
}

Is this solution even remotely correct? Also, is siftUp the correct name of the function?

Edit:

My new solution

void makeHeap(int A[], int n)
{ 
  for(int i = 1; i <= A[0]/2; i++)
    siftUp(A, i );
}

/* i- parent */ 
void siftUp(int A[], int i )
{
   bool done = false ; 
   int j = 2* i ; 
   while (i > 1 && !done)
   {
     if(A[j] > A[j+1]) // find the smallest child
       j+=1 ; 
     if(A[i] > A[j])
     {
       /* swap A[j] and A[i] */
       int temp = A[i]; 
       A[i] = A[j]; 
       A[j] = temp ; 

       j = i ; 
       i = i / 2 ; 


     } 
     else done = true ; 
   }
}
FutureSci
  • 1,750
  • 1
  • 19
  • 27
  • Your 'swap' code doesn't — so no, it is not remotely correct. It isn't clear to me that you need the `r` parameter to `siftUp()`; it is sufficient to know which node you are sifting up. You move from that leaf node (which starts as the rightmost node in the heap while you're sifting up) towards the root. You're lucky; the scheme with `A[0]` holding the size of the heap gives you a 1-based heap structure, which is simpler to code than a 0-based structure. – Jonathan Leffler Apr 25 '17 at 05:21
  • What you're doing is a series of inserts into the heap. You must insert all of the items. So your `makeHeap` loop termination condition has to be `i < n`, rather than `i <= n/2`. – Jim Mischel Apr 25 '17 at 14:41
  • but if i goes all the way up to n, then the sift up function won't execute half the time. i.e because j = i * 2 – FutureSci Apr 29 '17 at 04:46
  • @JonathanLeffler This came in an exam , so i was of the opinion that it would be a simple modification of the algorithm we studied in class. Hence I overlooked the fact that we don't need r. I think that this nieve assumption lead to my inability to tackle the problem correctly. Please see my edit, I have more faith in the corerectness of my solution now. – FutureSci Apr 29 '17 at 05:30

1 Answers1

1

Your code wouldn't heapify the array (ignoring errors like a and boolean)

For siftUp(), bear in mind the property parent(i) = i/2, where i is a node's index. Some pseudocode for inserting node into the heap (as the last node of the heap):

 Algo siftUp(H[], n)
     i = n
     while i > 1 and H[i] < H[i/2]
         swap(H[i], H[i/2])
         i = i / 2

This would result in O(nlogn) as you iterate through the array, but there is a better approach with the bottom up construction with O(n).

夢のの夢
  • 5,054
  • 7
  • 33
  • 63
  • Whereas the bottom-up construction is more efficient, it doesn't meet the last requirement: that items be processed in the order A[2], A[3], ... A[n-1]. – Jim Mischel Apr 25 '17 at 14:37
  • Thank you for your help. While your solution does not exactly meet the criteria specified, It did play a role in helping me to figure out the errors I've made. Please see the edited question for the new solution, if you would be so kind. – FutureSci Apr 29 '17 at 05:40