以最大堆Maxheap为底层的优先队列:
class MaxPQ<E>:IQueue<E> where E:IComparable<E>
{
private MaxHeap<E> heap;
public int Count {
get {
return heap.Count; } }
public bool IsEmpty {
get {
return heap.IsEmpty; } }
public MaxPQ(int capacity)
{
heap = new MaxHeap<E>(capacity);
}
public MaxPQ()
{
heap = new MaxHeap<E>();
}
public void Enqueue(E e)
{
heap.Insert(e);
}
public E Dequeue()
{
return heap.RemoveMax();
}
public E Peek()
{
return heap.Max();
}
public override string ToString()
{
return heap.ToString();
}
}
最小堆:
//最小堆
class MinHeap<E> where E : IComparable<E>
{
private E[] heap;
private int N;
public MinHeap(int capacity)
{
heap = new E[capacity + 1];
N = 0