Part Heap : Code Translation into C++ (heap.cpp) (#285)

* 添加heap章节C++版本关于heap的相关操作

* 完善C++版本的heap相关操作

* 完善C++版本的heap相关操作

* fix printHeap function
This commit is contained in:
LoneRanger
2023-02-04 14:35:45 +08:00
committed by GitHub
parent a95fe26303
commit 8e9eecd610
3 changed files with 137 additions and 0 deletions

View File

@ -13,6 +13,27 @@
#include "ListNode.hpp"
#include "TreeNode.hpp"
/**
* @brief Expose the underlying storage of the priority_queue container
*
* @tparam T
* @tparam S
* @tparam C
* @param pq
* @return S
*/
template <typename T, typename S, typename C>
S &Container(priority_queue<T, S, C> &pq) {
struct HackedQueue : private priority_queue<T, S, C>
{
static S &Container(priority_queue<T, S, C> &pq)
{
return pq.*&HackedQueue::c;
}
};
return HackedQueue::Container(pq);
}
class PrintUtil {
public:
/**
@ -293,4 +314,22 @@ class PrintUtil {
cout << kv.first << " -> " << kv.second << '\n';
}
}
/**
* @brief Print a Heap (PriorityQueue)
*
* @tparam T
* @tparam S
* @tparam C
* @param heap
*/
template <typename T, typename S, typename C>
static void printHeap(priority_queue<T, S, C> &heap) {
vector<T> vec = Container(heap);
cout << "堆的数组表示:" << endl;
printVector(vec);
cout << "堆的树状表示:" << endl;
TreeNode *root = vecToTree(vec);
printTree(root);
}
};