project-mergeheap

ordinary heap merge

#include <stdio.h>

#include <stdlib.h>

#include <time.h>/*time functions library*/

 

#ifndef _Heap_H

#define Max_Size (100000)/*the array-size of heap*/

#define K (100)/*merge K times*/

#define Range (1000)/*the data X's range*/

struct HeapStruct;

typedef struct HeapStruct *PriorityQueue;

typedef int ElementType;

PriorityQueue Initialize(int size);

void Insert(ElementType X,PriorityQueue H);

PriorityQueue OrdinaryHeapMerge(PriorityQueue H1,PriorityQueue H2);

#endif

 

struct HeapStruct/*data definition of Heap*/

{

int Size;

ElementType *Element;

};

 

PriorityQueue Initialize(int size)/*Initialize and create the heap*/

{

PriorityQueue H;

int X;

H=(PriorityQueue)malloc(sizeof(struct HeapStruct));

H->Element=(ElementType *)malloc(Max_Size*sizeof(ElementType));/*malloc enough space*/

 

H->Size=0;

/*create the heap*/

while(H->Size<size){

X=rand()%Range;/*get the X randly*/

Insert(X,H);/*insert X into the heap*/

}

 

return H;

}

 

void Insert(ElementType X,PriorityQueue H)/*by percolating up to insert X*/

{

int i;

for(i=++H->Size;H->Element[i/2]>X;i/=2)

H->Element[i]=H->Element[i/2];

H->Element[i]=X;

}

 

PriorityQueue OrdinaryHeapMerge(PriorityQueue H1,PriorityQueue H2)/*merge the two heaps*/

{

PriorityQueue H;

int i,j;

H=(PriorityQueue)malloc(sizeof(struct HeapStruct));

H->Element=(ElementType *)malloc((H1->Size+H2->Size)*sizeof(ElementType));/*apply another two space and we assume H1->Size+H2->Size<Max_Size*/

 

H->Size=0;i=j=0;

while(i<H1->Size&&j<H2->Size)/*merge when two heap are still have Element*/

{

if(H1->Element[i]<H2->Element[j])

Insert(H1->Element[i++],H);/*insert by percolating up*/

else

Insert(H2->Element[j++],H);

}

while(i<H1->Size)/*when one heap is over*/

Insert(H1->Element[i++],H);

while(j<H2->Size)

Insert(H2->Element[j++],H);

return H;

}

 

int main()

{

PriorityQueue H1,H2,H;

int m,n,x;

/*int i;*/

clock_t start,stop,ticks;

 

scanf("%d%d",&m,&n);

start=clock();/*get the start time*/

/*do the initialize and merge for K times*/

for(x=0;x<K;x++){

H1=Initialize(m);/*initialize the heap of size m*/

H2=Initialize(n);

 

H=OrdinaryHeapMerge(H1,H2);

/*

i=1;

while(i<=H->Size)

printf("%d ",H->Element[i++]);*/

}

stop=clock();/*get the stop time*/

ticks=stop-start;

printf("total time:%.6lf\n",(double)ticks/CLK_TCK/K);/*print out the total time*/

return 0;

}

 

skew heap merge

 

#include <stdio.h>

#include <stdlib.h>

#include <time.h>/*time functions library*/

 

#ifndef _Heap_H

struct TreeNode;

typedef struct TreeNode *PriorityQueue;

typedef int ElementType;

#define Range (1000)/*the data's range*/

#define K (100)/*merge K times*/

 

PriorityQueue Initialize(int size);

PriorityQueue Insert(ElementType X,PriorityQueue H);

PriorityQueue Merge(PriorityQueue H1,PriorityQueue H2);

PriorityQueue Merge1(PriorityQueue H1,PriorityQueue H2);

#endif

 

struct TreeNode/*data definition of Heap*/

{

ElementType Element;

PriorityQueue Left;

PriorityQueue Right;

int Npl;

};

 

void SwapChildren(PriorityQueue H)/*swap the H's children*/

{

PriorityQueue P;

 

P=H->Left;

H->Left=H->Right;

H->Right=P;

}

 

PriorityQueue Initialize(int size)/*Initialize and create the heap*/

{

PriorityQueue H;

int X,i;

H=NULL;

i=0;

while(i++<size)/*create the heap*/

{

X=rand()%Range;/*get the X randly*/

H=Insert(X,H);/*insert X into the heap*/

}

return H;

}

 

PriorityQueue Merge(PriorityQueue H1,PriorityQueue H2)/*merge H2 to H1*/

{

if(H1==NULL)

return H2;

if(H2==NULL)

return H1;

if(H1->Element<H2->Element)/*when H1's element is small than H2's then merge ,or change the order*/

return Merge1(H1,H2);

else

return Merge1(H2,H1);

}

 

static PriorityQueue Merge1(PriorityQueue H1,PriorityQueue H2)/*merge the heaps*/

{

if(H1->Left==NULL)/*if the H1's leftchild is empty then leftchild=H2*/

H1->Left=H2;

else

{

H1->Right=Merge(H1->Right,H2);/*merge H1's rightchild with H2*/

if(H1->Left->Npl<H1->Right->Npl)/*change H1's children based on the Npl*/

SwapChildren(H1);

 

H1->Npl=H1->Right->Npl+1;/*change the Npl*/

}

return H1;

}

 

PriorityQueue Insert(ElementType X,PriorityQueue H)/*insert the X by merging*/

{

PriorityQueue SingleNode;

 

SingleNode=(PriorityQueue)malloc(sizeof(struct TreeNode));/*malloce the space*/

SingleNode->Element=X;SingleNode->Npl=0;/*initialize the singlenode*/

SingleNode->Left=SingleNode->Right=NULL;

H=Merge(SingleNode,H);/*merge the singlenode with the original heap H*/

return H;

}

 

int main()

{

PriorityQueue H,H1,H2;

int m,n,x;

clock_t start,stop,ticks;

/*

PriorityQueue A[100];

int i,j;*/

 

scanf("%d%d",&m,&n);

start=clock();/*get the start time*/

/*do the initialize and merge for K times*/

for(x=0;x<K;x++){

H1=Initialize(m);/*initialize the heap of size m*/

    H2=Initialize(n);

 

H=Merge(H1,H2);

/*

A[0]=H;

i=0;j=1;

while(i<m+n){

if(A[i]->Left!=NULL)

A[j++]=A[i]->Left;

if(A[i]->Right!=NULL)

   A[j++]=A[i]->Right;

printf("%d ",A[i]->Element);

i++;

}*/

}

stop=clock();/*get the stop time*/

ticks=stop-start;

printf("total time:%.6lf\n",(double)ticks/CLK_TCK/K);/*print out the total time*/

return 0;

}

 

leftist heap merge

 

#include <stdio.h>

#include <stdlib.h>

#include <time.h>/*time functions library*/

 

#ifndef _Heap_H

struct TreeNode;

typedef struct TreeNode *PriorityQueue;

typedef int ElementType;

#define Range (100)/*the data's range*/

#define K (100)/*merge K times*/

 

PriorityQueue Initialize(int size);

PriorityQueue Insert(ElementType X,PriorityQueue H);

PriorityQueue Merge(PriorityQueue H1,PriorityQueue H2);

PriorityQueue Merge1(PriorityQueue H1,PriorityQueue H2);

#endif

 

struct TreeNode/*data definition of Heap*/

{

ElementType Element;

PriorityQueue Left;

PriorityQueue Right;

};

 

void SwapChildren(PriorityQueue H)/*swap the H's children*/

{

PriorityQueue P;

 

P=H->Left;

H->Left=H->Right;

H->Right=P;

}

 

PriorityQueue Initialize(int size)/*Initialize and create the heap*/

{

PriorityQueue H;

int X,i;

H=NULL;

i=0;

while(i++<size)/*create the heap*/

{

X=rand()%Range;/*get the X randly*/

H=Insert(X,H);/*insert X into the heap*/

}

return H;

}

 

PriorityQueue Merge(PriorityQueue H1,PriorityQueue H2)/*merge H2 to H1*/

{

if(H1==NULL)

return H2;

if(H2==NULL)

return H1;

if(H1->Element<H2->Element)/*when H1's element is small than H2's then merge ,or change the order*/

return Merge1(H1,H2);

else

return Merge1(H2,H1);

}

 

static PriorityQueue Merge1(PriorityQueue H1,PriorityQueue H2)/*merge the heaps*/

{

if(H1->Left==NULL)/*if the H1's leftchild is empty then leftchild=H2*/

H1->Left=H2;

else

{

H1->Right=Merge(H1->Right,H2);/*merge H1's rightchild with H2*/

if( H1->Right!=NULL)/*change H1's children when its rightchild is not null*/

SwapChildren(H1);

}

return H1;

}

 

PriorityQueue Insert(ElementType X,PriorityQueue H)/*insert the X by merging*/

{

PriorityQueue SingleNode;

 

SingleNode=(PriorityQueue)malloc(sizeof(struct TreeNode));/*malloce the space*/

SingleNode->Element=X;/*initialize the singlenode*/

SingleNode->Left=SingleNode->Right=NULL;

H=Merge(SingleNode,H);/*merge the singlenode with the original heap H*/

return H;

}

 

int main()

{

PriorityQueue H,H1,H2;

int m,n,x;

clock_t start,stop,ticks;

/*

PriorityQueue A[100];

int i,j;*/

 

scanf("%d%d",&m,&n);

start=clock();/*get the start time*/

/*do the initialize and merge for K times*/

for(x=0;x<K;x++){

H1=Initialize(m);/*initialize the heap of size m*/

    H2=Initialize(n);

 

H=Merge(H1,H2);

/*

A[0]=H;

i=0;j=1;

while(i<m+n){

if(A[i]->Left!=NULL)

A[j++]=A[i]->Left;

if(A[i]->Right!=NULL)

   A[j++]=A[i]->Right;

printf("%d ",A[i]->Element);

i++;

}*/

}

stop=clock();/*get the stop time*/

ticks=stop-start;

printf("total time:%.6lf\n",(double)ticks/CLK_TCK/K);/*print out the total time*/

return 0;

}

 

 

posted @ 2011-01-11 15:26  楚夕  阅读(375)  评论(0编辑  收藏  举报