下面程序是最小堆。
#include <iostream>
using namespace std;
template<class Elem>
class MinHeap
{
private:
Elem* heapArray;
int CurrentSize;
int MaxSize;
public:
MinHeap(const int n);
virtual~ MinHeap()
{
delete []heapArray;
}
void BuildHeap();
bool isLeaf(int pos) const;
int leftChild(int pos) const;
int rightChild(int pos) const;
//return parent position
int parent(int pos) const;
//删除给定下标的元素。
bool Remove(int pos,Elem& node);
void SiftDown(int left);
void SiftUp(int position);
bool Insert(const Elem& newNode);
Elem& RemoveMin();
bool print() const;
};
template<class Elem>
MinHeap<Elem

这是一个使用C++编写的最小堆优先队列模板类的实现,包括构造、插入、删除、调整堆等操作。示例展示了如何创建、插入元素、删除最小元素以及按位置删除元素的过程。
最低0.47元/天 解锁文章
2773





