mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-22 15:53:50 +08:00
Part Heap : Code Translation into C++ (heap.cpp) (#285)
* 添加heap章节C++版本关于heap的相关操作 * 完善C++版本的heap相关操作 * 完善C++版本的heap相关操作 * fix printHeap function
This commit is contained in:
@ -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);
|
||||
}
|
||||
};
|
||||
|
Reference in New Issue
Block a user