以最大堆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;
}
public MinHeap() : this(10) { }
public int Count { get { return N; } }
public bool IsEmpty { get { return N == 0; } }
//往堆中插入元素
public void Insert(E e)
{
if (N == (heap.Length - 1))
ResetCapacity(heap.Length * 2);
heap[N + 1] = e;
N++;
Swim(N);
}
//删除堆顶元素
public E RemoveMin()
{
if (IsEmpty)
throw new ArgumentException("堆为空!");
Swap(1, N);
E max = heap[N];
heap[N] = default(E);
N--;
Sink(1);
if (N == (heap.Length - 1) / 4)
ResetCapacity(heap.Length / 2);
return max;
}
//查看最小值
public E Min()
{
if (IsEmpty)
throw new ArgumentException("堆为空!");
return heap[1];
}
//输出数组类的信息
public override string ToString()
{
StringBuilder res = new StringBuilder();
res.Append("[");
for (int i = 1; i <= N; i++)
{
res.Append(heap[i]);
if (i != N)
res.Append(", ");
}
res.Append("]");
return res.ToString();
}
//调整数组容量的大小
private void ResetCapacity(int newCapacity)
{
E[] newHeap = new E[newCapacity];
for (int i = 1; i <= N; i++)
newHeap[i] = heap[i];
heap = newHeap;
}
//元素上游
private void Swim(int k)
{
while (k > 1 && heap[k].CompareTo(heap[k / 2]) < 0)
{
Swap(k, k / 2);
k = k / 2;
}
}
//元素下沉
private void Sink(int k)
{
while (2 * k <= N)
{
int j = 2 * k;
if (j + 1 <= N && heap[j + 1].CompareTo(heap[j]) < 0) j++;
if (heap[k].CompareTo(heap[j]) <= 0) break;
Swap(k, j);
k = j;
}
}
private void Swap(int i, int j)
{
E e = heap[i];
heap[i] = heap[j];
heap[j] = e;
}
}
以最小堆Minheap为底层的优先队列:
class MinPQ<E> : IQueue<E> where E : IComparable<E>
{
private MinHeap<E> heap;
public int Count { get { return heap.Count; } }
public bool IsEmpty { get { return heap.IsEmpty; } }
public MinPQ(int capacity)
{
heap = new MinHeap<E>(capacity);
}
public MinPQ()
{
heap = new MinHeap<E>();
}
public void Enqueue(E e)
{
heap.Insert(e);
}
public E Dequeue()
{
return heap.RemoveMin();
}
public E Peek()
{
return heap.Min();
}
public override string ToString()
{
return heap.ToString();
}
}
class Program
{
static void Main(string[] args)
{
int[] a = TestSearch.ReadFile("测试文件3/TopM.txt");
QuickSort3.Sort(a);
for (int i = 0; i < 10; i++)
Console.Write(a[i] + ", ");
Console.WriteLine();
MaxPQ<int> pq = new MaxPQ<int>(10);
FileStream fs = new FileStream("测试文件3/TopM.txt",FileMode.Open);
StreamReader sr = new StreamReader(fs);
while (!sr.EndOfStream)
{
int value = int.Parse(sr.ReadLine());
if (pq.Count < 10)
pq.Enqueue(value);
else if (value < pq.Peek())
{
pq.Dequeue();
pq.Enqueue(value);
}
}
Console.WriteLine(pq);
Console.Read();
}
}