コード例 #1
0
        public int Pop()
        {
            HeapNode result = _innerList[0];
            int      p = 0, p1, p2, pn;

            _innerList[0] = _innerList[_innerList.Count - 1];
            _innerList.RemoveAt(_innerList.Count - 1);
            do
            {
                pn = p;
                p1 = (p << 1) + 1;
                p2 = (p << 1) + 2;
                if (_innerList.Count > p1 && _innerList[p].Weight > _innerList[p1].Weight)
                {
                    p = p1;
                }
                if (_innerList.Count > p2 && _innerList[p].Weight > _innerList[p2].Weight)
                {
                    p = p2;
                }

                if (p == pn)
                {
                    break;
                }

                HeapNode h = _innerList[p];
                _innerList[p]  = _innerList[pn];
                _innerList[pn] = h;
            } while (true);
            return(result.Index);
        }
コード例 #2
0
        public void Push(int index, float weight)
        {
            int p = _innerList.Count, p2;

            _innerList.Add(new HeapNode(index, weight));
            do
            {
                if (p == 0)
                {
                    break;
                }

                p2 = (p - 1) >> 1;
                if (_innerList[p].Weight < _innerList[p2].Weight)
                {
                    HeapNode h = _innerList[p];
                    _innerList[p]  = _innerList[p2];
                    _innerList[p2] = h;

                    p = p2;
                }
                else
                {
                    break;
                }
            } while (true);
        }