#include <iostream>
#include "min_max_heap.h"
#include <time.h>
#include <stdlib.h>
using namespace std;
int* create_array(const int n);
void main()
{
int n;
cin>>n;
while(n>0)
{
int* a = create_array(n);
cout<<"The array: ";
for(int i=0; i<n; i++)
cout<<a[i]<<" ";
cout<<endl;
min_max_heap hp(a,n);
cout<<"The min-max heap: "<<hp<<endl;
cout<<"delmax(): ";
for(int i=0; i<n; i++)
cout<<hp.delmax()<<" ";
cout<<endl;
for(int i=0; i<n; i++)
hp.insert(a[i]);
cout<<"The min-max heap: "<<hp<<endl;
cout<<"delmin(): ";
for(int i=0; i<n; i++)
cout<<hp.delmin()<<" ";
cout<<endl;
cin>>n;
}
}
int* create_array(const int n)
{
int* res = new int[n];
for(int i=0; i<n; i++)
res[i] = 0;
for(int i=0; i<n; i++)
{
srand((unsigned)time(0));
while(1)
{
int m=rand()%n;
if(res[m] ==0)
{
res[m] = i;
break;
}
}
}
return res;
}
上述代码只是我在学习min-max heap 时使用的测试代码,如有什么不明白的地方欢迎讨论。