0

I just tried to build a min heap and i am getting different answers for building a heap

Method 1 insert elements into the array and then call a build minheap method which applies minHeapify on the internal nodes.

Method 2 insert elements directly into the heap by checking at every point if if the array follows minheap property or not.

Both the answers are correct i guess but is it okay if some test case shows one order and the ans is different.

#include<iostream>
#include<cmath>
#include<vector>
using namespace std;
int leftChild(int i)
{
    return (2*i+1);
}
int rightChild(int i)
{
    return (2*i+2);
}
int parent(int i)
{
    return ((i-1)/2);
}
void minHeapify(vector<int> &a,int i)
{
    int smallest = i;
    int l = leftChild(i);
    int r = rightChild(i);
    if(l < a.size() && a[l] < a[i])
        smallest = l;
    if(r < a.size() && a[r] < a[smallest])
        smallest = r;
    if(smallest != i)
    {
        swap(a[smallest],a[i]);
        minHeapify(a,smallest);
    }
}
void buildMinHeap(vector<int> &a)
{
    for(int i = a.size()/2 - 1 ; i >= 0 ; i--)
        minHeapify(a,i);
}
void insertKey(vector<int> &b,int new_val,int* n)
{
    *n = *n + 1;
    int i = *n - 1;
    b[i] = new_val;
    while(i!=0 && b[i] < b[parent(i)])
    {
        swap(b[i],b[parent(i)]);
        i = parent(i);
    }
}
void printline()
{
    cout<<"********************************************************************"<<endl;
}
int main()
{
    cout<<"Enter the elements in the array for building a min heap"<<endl;
    vector<int> a;
    a.push_back(2);
    a.push_back(16);
    a.push_back(74);
    a.push_back(58);
    a.push_back(36);
    a.push_back(4);
    a.push_back(28);
    a.push_back(15);
    a.push_back(35);
    a.push_back(82);
    a.push_back(6);

    //One thing you can do is make a normal array and build a heap out of it in O(n) time
    buildMinHeap(a);
    for(int i=0;i<a.size();i++)
        cout<<a[i]<<" ";
    cout<<endl<<endl;
    printline();

    //Or second method is insert in a way such that it is always inserted in a form of heap.
    vector<int> b(10000);
    int heapsize = 0;
    insertKey(b,2,&heapsize);
    insertKey(b,16,&heapsize);
    insertKey(b,74,&heapsize);
    insertKey(b,58,&heapsize);
    insertKey(b,36,&heapsize);
    insertKey(b,4,&heapsize);
    insertKey(b,28,&heapsize);
    insertKey(b,15,&heapsize);
    insertKey(b,35,&heapsize);
    insertKey(b,82,&heapsize);
    insertKey(b,6,&heapsize);
    for(int i=0;i<heapsize;i++)
        cout<<b[i]<<" ";
}

I checked using the min_heap function and constructed a min heap from both my answers. min_heap(a.begin(),a.end(),myComparator()) yields the same ans as yielded by method1 and min_heap(b.begin(),b.end(),myComparator()) yields the same ans yielded by method2. So I just wanted to confirm is this thing alright???

anshul6297
  • 25
  • 4

1 Answers1

0

For a Heap, the order of elements saved in the underlying data structure is not important.

You should check that the popped values from the Min Heap is in ascending order i.e. the root element is the minimum at all times.

Add top() and pop() methods to both your structures. top() will simply return the root element value and pop() will erase the root element and replace it with a suitable element from the Heap.

The following code should print elements in the same order.

while(heap.size() > 0)
{
std::cout<<heap.top()<<" ";
heap.pop();
}

For coding competitions, they will never check the order of the underlying structure. You are supposed to print them in order.

shainu vy
  • 181
  • 1
  • 8