mirror of
https://github.com/krahets/hello-algo.git
synced 2025-01-23 22:40:25 +08:00
Update heap.cpp
This commit is contained in:
parent
8e9eecd610
commit
6ca5fa7d93
@ -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)
|
||||
|
@ -326,7 +326,7 @@ class PrintUtil {
|
||||
template <typename T, typename S, typename C>
|
||||
static void printHeap(priority_queue<T, S, C> &heap) {
|
||||
vector<T> vec = Container(heap);
|
||||
cout << "堆的数组表示:" << endl;
|
||||
cout << "堆的数组表示:";
|
||||
printVector(vec);
|
||||
cout << "堆的树状表示:" << endl;
|
||||
TreeNode *root = vecToTree(vec);
|
||||
|
Loading…
Reference in New Issue
Block a user