最小堆,执行不出结果
#include<iostream.h>void swap(int &a,int &b){int c;c=a;a=b;b=a;}
class minheap{
private:
int a[100];
int csize,msize;
public:
minheap(int b[],int m);
void siftdown(int parent);
void siftup(int j);
void buildheap();
void output();};
minheap::minheap(int b[],int m)
{int i;
for(i=0;i<m;i++) a[i]=b[i];
csize=m;}
void minheap::siftdown(int parent)
{int i=parent;int j;
j=2*i+1; //j为i的左孩子
while(j<csize){
if((j<csize-1) && (a[j]>a[j+1]))
j++;
if(a[i]>a[j]){
swap(a[i],a[j]);
i=j;
i=2*j+1;}
else break;}}
void minheap::siftup(int j)
{int p=(j-1)/2;
while((j>0) && (a[p]>a[j])){
swap(a[j],a[p]);
j=p;}}
void minheap::buildheap(){
for(int i=csize/2-1;i>=0;i++)
siftdown(i);}
void minheap::output(){
for(int i=0;i<csize;i++)
cout<<a[i]<<' ';
cout<<endl;}
void main()
{int a[10]={14,25,20,17,24,22,26,18,15,12};
minheap A(a,10);
A.buildheap(); A.output();}