|
|
|
@ -9,14 +9,14 @@
|
|
|
|
|
void testPush(priority_queue<int> &heap, int val)
|
|
|
|
|
{
|
|
|
|
|
heap.push(val); // 元素入堆
|
|
|
|
|
cout << "元素 " << val << " 入堆后" << endl;
|
|
|
|
|
cout << "\n元素 " << val << " 入堆后" << endl;
|
|
|
|
|
PrintUtil::printHeap(heap);
|
|
|
|
|
}
|
|
|
|
|
void testPoll(priority_queue<int> &heap)
|
|
|
|
|
{
|
|
|
|
|
int val = heap.top();
|
|
|
|
|
heap.pop();
|
|
|
|
|
cout << "堆顶元素 " << val << " 出堆后" << endl;
|
|
|
|
|
cout << "\n堆顶元素 " << val << " 出堆后" << endl;
|
|
|
|
|
PrintUtil::printHeap(heap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -28,7 +28,7 @@ int main()
|
|
|
|
|
// 初始化大顶堆
|
|
|
|
|
priority_queue<int, vector<int>, less<int>> maxHeap;
|
|
|
|
|
|
|
|
|
|
cout << "以下测试样例为大顶堆" << endl;
|
|
|
|
|
cout << "\n以下测试样例为大顶堆" << endl;
|
|
|
|
|
|
|
|
|
|
/* 元素入堆 */
|
|
|
|
|
testPush(maxHeap, 1);
|
|
|
|
@ -39,7 +39,7 @@ int main()
|
|
|
|
|
|
|
|
|
|
/* 获取堆顶元素 */
|
|
|
|
|
int peek = maxHeap.top();
|
|
|
|
|
cout << "堆顶元素为: " << peek << endl;
|
|
|
|
|
cout << "\n堆顶元素为 " << peek << endl;
|
|
|
|
|
|
|
|
|
|
/* 堆顶元素出堆 */
|
|
|
|
|
testPoll(maxHeap);
|
|
|
|
@ -50,11 +50,11 @@ int main()
|
|
|
|
|
|
|
|
|
|
/* 获取堆大小 */
|
|
|
|
|
int size = maxHeap.size();
|
|
|
|
|
cout << "堆元素数量为: " << size << endl;
|
|
|
|
|
cout << "\n堆元素数量为 " << size << endl;
|
|
|
|
|
|
|
|
|
|
/* 判断堆是否为空 */
|
|
|
|
|
bool isEmpty = maxHeap.empty();
|
|
|
|
|
cout << "堆是否为空: " << isEmpty << endl;
|
|
|
|
|
cout << "\n堆是否为空 " << isEmpty << endl;
|
|
|
|
|
|
|
|
|
|
/* 输入列表并建堆 */
|
|
|
|
|
// 时间复杂度为 O(n) ,而非 O(nlogn)
|
|
|
|
|