0

I am trying to write a program to represent a min heap using an array. I know that the min heap is similar to a tree data structure where the root node is smaller than it's children. Here is my code:-

# include <stdio.h>
# include <math.h>

int leftChild(int i)
{
    return 2 * i + 1;
}

int rightChild(int i)
{
    return 2 * i + 2;
}

void minHeap(int Arr[], int n, int i)
{
    int l, r, Least;
    l = leftChild(i);
    r = rightChild(i);
    Least = i;

    if(l < n && Arr[l] < Arr[Least])
        Least = l;

    if(r < n && Arr[r] < Arr[Least])
        Least = r;

    if(Least != i)
    {
        int Temp = Arr[i];
        Arr[i] = Arr[Least];
        Arr[Least] = Temp;
        minHeap(Arr, n, Least);
    }
}

int main(void) 
{
    int n;
    printf("\nEnter number of elements : ");
    scanf("%d", &n);

    printf("\nEnter The Elements : ");
    int Arr[n];

    for(int i = 0 ; i < n ; i++)
    {
       scanf("%d", &Arr[i]);
    }

    for(int i = n / 2 - 1 ; i >= 0 ; i--)
      minHeap(Arr, n, i);

    printf("\nMin Heap : ");

    for(int i = 0 ; i < n ; i++)
     printf("%d ", Arr[i]);
    return 0;
}


Input : 
Enter number of elements : 5
Enter the elements : 90 70 10 30 50
Output : 10 30 90 70 50 

But, when I try building the heap on paper, this is the output I get:-

Expected Output : 10 30 70 90 50

Can someone point out the error here for me?

Neel
  • 131
  • 3
  • 9
  • You can look at this implementation: https://gist.github.com/sudhanshuptl/d86da25da46aa3d060e7be876bbdb343 – klutt Oct 28 '19 at 11:14
  • You always swap your parent with the right child,so your program works good – pandy giankoulidis Oct 28 '19 at 11:22
  • @pandygiankoulidis, so what do you suggest I do? – Neel Oct 28 '19 at 11:38
  • @klutt, the implementation in the link does not help. I am still not getting the expected output. – Neel Oct 28 '19 at 11:38
  • So, what, exactly, is your code supposed to be doing? Building a heap usually involves insertion one at a time with a bottom-to-top step for each insertion - I don't see that, but I could be missing something. The top-to-bottom step is usually done when removing the smallest item. So, I think some comments in your code as to what you are doing and why, and also some indication of what you have done on paper to build the heap. FWIW your paper list looks right to me for insertion one at a time. – Euan Smith Oct 28 '19 at 11:43
  • I think you got the last step wrng when building your heap on paper: The 90 gets swapped with the 10, not with the 30. The result is your actual output. – M Oehm Oct 28 '19 at 11:48

2 Answers2

2

The output given by your code is a valid output.

Now, you will get your expected output when you individually add each element to min heap.

Here, you are trying it on the array as a whole. That's why you got confused.

Adding every element individually to min heap and min heapifying an array as a whole, are the 2 scenarios due to which you got confused.

Deepak Tatyaji Ahire
  • 4,883
  • 2
  • 13
  • 35
  • How do I individually add elements to a heap?? – Neel Oct 28 '19 at 14:08
  • If you have a heap size N using elements 0..N-1, usually, you add a new element to your array at N then bubble it in. The heap will never interact with elements above N. If you already have an array of data, You bubble in element 1 to the existing 1-element heap, then bubble in element 2 to the 2-element heap, and so-on until all your array is bubbled in, at which point your array is completely heapified. Interesting note: An existing sorted array is a valid heap, though it may need to be reversed. – Gem Taylor Oct 28 '19 at 16:41
1

Your program is working correctly. It's your calculation which is at fault.
A simple visualisation of the steps is as follows.

    90     heapify(Arr,1)
   /  \    swap(1,3)
 >70   10   
 / \
30  50



   >90     heapify(Arr,0)
   /  \    swap(0,2)
  30   10   
 / \
70  50



    10   
   /  \   
  30   90   
 / \
70  50

The resultant minheap should be 10 30 90 70 50

rsonx
  • 436
  • 7
  • 16