1

My aim is to construct a binomial heap. Here is my code which i have written right now:

#include<iostream>
using namespace std;
void maxheapify(int a[],int length,int i)
{
    int left=2*i;
    int right=2*i+1;
    int largest=i;
    if(left<length && a[left]>a[largest])
    {
        largest=left;

    }
    if ( right<length && a[right]>a[largest])
    {
        largest=right;
    }

    if(largest!=i)
    {
int temp=a[i];
a[i]=a[largest];
a[largest]=temp;
maxheapify(a,length,largest);

    }

}
void buildmax(int a[],int length)
{
    for(int i=(length-1)/2;i>=0;i--)
    {
        maxheapify(a,length,i);

    }


}
/*void heapsort(int a[],int length)
{
    buildmax(a,length);
    for(int i=length-1;i>0;i--)
    {
        int temp=a[i];
        a[i]=a[0];
        a[0]=temp;
        maxheapify(a,i,0);


    }


}
*/
 void combine_heap(int a[],int n,int b[],int m,int c[])
 {

 }
int main()
{
    int a[100];
    int n=sizeof(a)/sizeof(a[0]);

    int b[100];
    int m=sizeof(b)/sizeof(b[0]);
    int c[200];
    int length=sizeof(a)/sizeof(a[0]);
    for(int i=0;i<length;i++){
        a[i]=34+rand()%(length-33);
         b[i]=rand()%(i+1);
            }
    /*heapsort(a,length);*/
    /*for(int i=0;i<length;i++)
        cout<<a[i]<<"  ";*/


    return 0;
}

i think trivial solution would be to combine two array into third one and then call buildmax procedure,but i think it is not efficient,i have tried to implement this pseudo code from wikipedia

function merge(p, q)
    while not( p.end() and q.end() )
        tree = mergeTree(p.currentTree(), q.currentTree())
        if not heap.currentTree().empty()
            tree = mergeTree(tree, heap.currentTree())
            heap.addTree(tree)
        else
            heap.addTree(tree)
        heap.next() p.next() q.next()

but i dont know how to implement it,because in generally how to access subtrees?another variant is construct priority queue and by using insert function insert first from one array and then from another array,but is this optimal?please help me to write code to combine these two max heap into one efficiently

Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319
  • 1
    We can help you debug any problems you may have, but we will not write the code for you. We're not a code writing company. – Tony The Lion Sep 04 '12 at 11:41
  • no not write code for me,help me how to write –  Sep 04 '12 at 11:42
  • well, what have you written and tried? – Tony The Lion Sep 04 '12 at 11:42
  • i think what should be optimal way for combine this two max heap,and i have problem according to pseudo code,how to access subtrees in array?pseudo code seems to be difficult and i am searching alternative ways –  Sep 04 '12 at 11:45
  • 1
    Wikipedia has all the information on how to implement it efficiently - http://en.wikipedia.org/wiki/Binomial_heap – ronalchn Sep 04 '12 at 11:56
  • but pseudo code is difficult,i need to translate it in language –  Sep 04 '12 at 12:01
  • if we implement priority queue and insert,is not it optimal? –  Sep 04 '12 at 12:03
  • Pseudo code is a description in human language, and that you find difficult? Seems hard to believe. That said, since you want to write C++, start with using a vector instead of raw pointers. Also, write tests, i.e. test-driven development. – Ulrich Eckhardt Jul 26 '14 at 07:19

1 Answers1

0

This is a good example of Binomial Heap but it is in c. You will get the basic logic to implement binomial heap. See Example here

Or get video tutorial to here to understand algorithm.

Md Mohsin
  • 1,568
  • 1
  • 18
  • 28