演算法筆記(堆實現的最大優先隊列)

來源:互聯網
上載者:User

採用最大堆實現的優先隊列,QueueElement是隊列元素類,包含一個衛星資料成員和一個權重Key值:

 
public class QueueElement<T>    {        public int KeyValue{get;internal set;}        public T Element{get;private set;}        public QueueElement(T Item,int KeyVal)        {            KeyValue = KeyVal;            Element = Item;        }    }    public class MaxHeapQueue<T>     {        private List<QueueElement<T>> _queueValues = new List<QueueElement<T>>();        public int Count        {            get            {                return _queueValues.Count;            }        }        public T GetMaximum()        {            return _queueValues[0].Element;        }        public T ExtractMax()        {            if (_queueValues.Count <= 0)            {                throw new Exception("隊列為空白");            }            T theMax = _queueValues[0].Element;            int theTail = Count -1;            _queueValues[0] = _queueValues[theTail];            _queueValues.RemoveAt(theTail);            MaxHeapify(0);            return theMax;        }        public void MaxHeapify(int i)        {            int HeapSize = Count;            int theL = HeapL(i);            int theR = HeapR(i);            int thelargest = i;            if (theL < HeapSize && _queueValues[theL].KeyValue> _queueValues[thelargest].KeyValue)            {                thelargest = theL;            }            if (theR < HeapSize && _queueValues[theR].KeyValue> _queueValues[thelargest].KeyValue)            {                thelargest = theR;            }            if (thelargest != i)            {                SwapElement(i, thelargest);                MaxHeapify(thelargest);            }        }        public void IncreaseKey(Func<T, bool> SelectFunc, int NewKey)        {            int theIndex = -1;            for (int i = 0; i < Count; i++)            {                if (SelectFunc(_queueValues[i].Element) == true)                {                    theIndex = i;                    break;                }            }            if (theIndex < 0)            {                return;            }            if (_queueValues[theIndex].KeyValue >= NewKey)            {                return;            }            _queueValues[theIndex].KeyValue = NewKey;            UpAdjust(theIndex);        }        private void UpAdjust(int i)        {            int theIndex = i;            int thePIndex = HeapP(theIndex);            while (thePIndex >= 0 && _queueValues[theIndex].KeyValue > _queueValues[thePIndex].KeyValue)            {                SwapElement(thePIndex, theIndex);                theIndex = thePIndex;                thePIndex = HeapP(theIndex);            }        }        public void IncreaseKey(int i, int NewKey)        {            int theIndex = i;            if (_queueValues[theIndex].KeyValue >= NewKey)            {                return;            }            _queueValues[theIndex].KeyValue = NewKey;            int thePIndex = HeapP(theIndex);            while (thePIndex >= 0 && _queueValues[theIndex].KeyValue > _queueValues[thePIndex].KeyValue)            {                SwapElement(thePIndex, theIndex);                theIndex = thePIndex;                thePIndex = HeapP(theIndex);            }        }        public void HeapDelete(Func<T, bool> SelectFunc)        {            int theIndex = -1;            for (int i = 0; i < Count; i++)            {                if (SelectFunc(_queueValues[i].Element) == true)                {                    theIndex = i;                    break;                }            }            if (theIndex < 0)            {                return;            }            SwapElement(theIndex, Count - 1);            _queueValues.RemoveAt(Count-1);            if (theIndex < Count)            {                int theP = HeapP(theIndex);                bool theUp = false;                if (theP >= 0)                {                    if (_queueValues[theIndex].KeyValue > _queueValues[theP].KeyValue)                    {                        UpAdjust(theIndex);                        theUp = true;                    }                }                if(theUp==false)                {                    MaxHeapify(theIndex);                }            }                    }        private void SwapElement(int i, int j)        {            QueueElement<T> theTmp = _queueValues[i];            _queueValues[i] = _queueValues[j];            _queueValues[j] = theTmp;        }        public void HeapInsert(T Element, int Key)        {            _queueValues.Add(new QueueElement<T>(Element, int.MinValue));            IncreaseKey(Count-1, Key);        }        private int HeapL(int i)        {            return i * 2 + 1;        }        private  int HeapR(int i)        {            return i * 2 + 2;        }        private  int HeapP(int i)        {            return (i + 1) / 2 - 1;        }
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.