maxheap.cpp

来自「这是数据结构、算法与应用-C++语言描述的代码」· C++ 代码 · 共 47 行

CPP
47
字号
// test max heap

#include <iostream>
#include "maxHeap.h"

using namespace std;

int main(void)
{
   // test constructor and push
   maxHeap<int> h(4);
   h.push(10);
   h.push(20);
   h.push(5);

   cout << "Heap size is " << h.size() << endl;
   cout << "Elements in array order are" << endl;
   cout << h << endl;

   h.push(15);
   h.push(30);

   cout << "Heap size is " << h.size() << endl;
   cout << "Elements in array order are" << endl;
   cout << h << endl;

   // test top and pop
   cout << "The max element is " << h.top() << endl;
   h.pop();
   cout << "The max element is " << h.top() << endl;
   h.pop();
   cout << "The max element is " << h.top() << endl;
   h.pop();
   cout << "Heap size is " << h.size() << endl;
   cout << "Elements in array order are" << endl;
   cout << h << endl;

   // test initialize
   int z[10];
   for (int i = 1; i < 10; i++)
      z[i] = i;
   h.initialize(z, 9);
   cout << "Elements in array order are" << endl;
   cout << h << endl;
   return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?