用 C# 實現優先隊列

來源:互聯網
上載者:User

優先隊列(priority queue) 是很重要的資料結構。我在做 ACM 題時就經常要用到她。C++ STL 就包括 priority_queue 。Java 也有 PriorityQueue 類。遺憾的是,.NET Framework Base Class Library 中並不包括優先隊列。於是,我只好自己用 C# 語言寫一個,如下所示:

using System;using System.Collections.Generic;namespace Skyiv.Util{  class PriorityQueue<T>  {    IComparer<T> comparer;    T[] heap;    public int Count { get; private set; }    public PriorityQueue() : this(null) { }    public PriorityQueue(int capacity) : this(capacity, null) { }    public PriorityQueue(IComparer<T> comparer) : this(16, comparer) { }    public PriorityQueue(int capacity, IComparer<T> comparer)    {      this.comparer = (comparer == null) ? Comparer<T>.Default : comparer;      this.heap = new T[capacity];    }    public void Push(T v)    {      if (Count >= heap.Length) Array.Resize(ref heap, Count * 2);      heap[Count] = v;      SiftUp(Count++);    }    public T Pop()    {      var v = Top();      heap[0] = heap[--Count];      if (Count > 0) SiftDown(0);      return v;    }    public T Top()    {      if (Count > 0) return heap[0];      throw new InvalidOperationException("優先隊列為空白");    }    void SiftUp(int n)    {      var v = heap[n];      for (var n2 = n / 2; n > 0 && comparer.Compare(v, heap[n2]) > 0; n = n2, n2 /= 2) heap[n] = heap[n2];      heap[n] = v;    }    void SiftDown(int n)    {      var v = heap[n];      for (var n2 = n * 2; n2 < Count; n = n2, n2 *= 2)      {        if (n2 + 1 < Count && comparer.Compare(heap[n2 + 1], heap[n2]) > 0) n2++;        if (comparer.Compare(v, heap[n2]) >= 0) break;        heap[n] = heap[n2];      }      heap[n] = v;    }  }}

如上所示,這個 PriorityQueue<T> 泛型類提供四個公用建構函式,第一個是無參的建構函式,其餘的建構函式允許指定優先隊列中包括的初始元素數(capacity)、如何對鍵進行比較(comparer)。

這個程式使用堆(heap)來實現優先隊列。所以,所需的空間是最小的。Count 屬性和 Top 方法的時間複雜度是 O(1),Push 和 Pop 方法的時間複雜度都是 O(logN)。

我曾經用 List<T> 泛型類實現過一個優先隊列,請參見我的部落格 Timus1016. A Cube on the Walk 。雖然更簡單,程式碼只有 23 行,但是效率不高,其 Push 和 Pop 方法的時間複雜度都是 O(N)。

演算法和資料結構目錄
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.