Example #1
0
int main(int arg, char* argv[])
{
    int dummy;
    MaxHeap<int> mh;

    mh.Insert(9).Insert(3).Insert(8).Insert(2).Insert(7).Insert(6).Insert(4).Insert(5).Insert(1);
    cout << "MaxHeap elements by level: " << mh << endl;
    mh.PrintTreeVertically(cout, 60);

    mh.DeleteMax(dummy);
    cout << "MaxHeap elements by level: " << mh << endl;
    mh.PrintTreeVertically(cout, 60);

    mh.DeleteMax(dummy);
    cout << "MaxHeap elements by level: " << mh << endl;
    mh.PrintTreeVertically(cout, 40);

    mh.DeleteMax(dummy);
    cout << "MaxHeap elements by level: " << mh << endl;
    mh.PrintTreeVertically(cout, 40);

    // Sorting
    int a[] = { 0, 1, 4, 8, 9, 2, 1, 4, 3, 5, 7, 6, 3 };
#define ELEM_COUNT(a)   (sizeof(a) / sizeof(a[0]))

    MaxHeap<int>::Sort(a, ELEM_COUNT(a));
    cout << "Sorting with MaxHeap:" << endl;
    for (int i = 0; i < ELEM_COUNT(a); i++) {
        cout << a[i] << ", ";
    }
    cout << endl;

    return 0;
}
int main()
{
    MaxHeap<int> heap;
    int n=5;
    cout<<"初始数组大小:"<<n<<endl;;
    //cin>>n;
    cout<<"输入的数组:";
    //a = new int[n];
    int a[6]={0,5,4,11,3,7};
    for(int i=1;i<=n;i++)
        //cin>>a[i];
        cout<<a[i]<<" ";
    cout<<endl;
    cout<<"初始化为最大堆:"<<endl;
    heap.Initialize(a,n,100);
    heap.Output();
    cout<<"\n插入10之后的最大堆:\n";
    heap.Insert(10);
    heap.Output();
    cout<<"当前堆的大小:"<<heap.Size()<<endl;
    cout<<"\n删除最大数之后的最大堆:\n";
    int b=0;
    heap.DeleteMax(b);
    heap.Output();
    cout<<"当前堆的大小:"<<heap.Size()<<endl;
	getchar();
    return 0;
}
Example #3
0
	void AddLiveNode(float up, float cp, int cw, bool ch, int level){
		bbnode * b=new bbnode;
		b->parent=E;
		b->LChild=ch;
		HeapNode N;
		N.uprofit=up;
		N.profit=cp;
		N.weight=cw;
		N.level=level;
		N.ptr=b;
		H->Insert(N);
	}