コード例 #1
0
        public void DecreaseKey(int index, T item)
        {
            int p = qp[index];

            if (SortUtil.IsGreaterThan(keys[index], item))
            {
                keys[index] = item;
                Swim(p);
            }
        }
コード例 #2
0
        public int DelMin()
        {
            var item = pq[1];

            SortUtil.Exchange(pq, 1, N);
            qp[pq[1]] = 1;
            qp[pq[N]] = N;
            N--;
            Sink(1);
            return(item);
        }
コード例 #3
0
        private void Swim(int k)
        {
            while (k > 1)
            {
                var parent = k / 2;
                if (SortUtil.IsLessThan(keys[pq[k]], keys[pq[parent]]))
                {
                    SortUtil.Exchange(pq, k, parent);

                    qp[pq[k]]      = k;
                    qp[pq[parent]] = parent;

                    k = parent;
                }
                else
                {
                    break;
                }
            }
        }
コード例 #4
0
        public void Enqueue(int index, T item)
        {
            var p = qp[index];

            if (p == -1)
            {
                Insert(index, item);
            }
            else
            {
                if (SortUtil.IsLessThan(keys[index], item))
                {
                    keys[index] = item;
                    Sink(p);
                }
                else
                {
                    keys[index] = item;
                    Swim(p);
                }
            }
        }
コード例 #5
0
 private void Sink(int k)
 {
     while (k * 2 <= N)
     {
         int child = 2 * k;
         if (child < N && SortUtil.IsLessThan(keys[pq[child + 1]], keys[pq[child]]))
         {
             child++;
         }
         if (SortUtil.IsLessThan(keys[pq[child]], keys[pq[k]]))
         {
             SortUtil.Exchange(pq, child, k);
             qp[pq[child]] = child;
             qp[pq[k]]     = k;
             k             = child;
         }
         else
         {
             break;
         }
     }
 }