Ejemplo n.º 1
0
        /***************************************************************************
        * Helper functions to restore the heap invariant.
        ***************************************************************************/

        private void swim(int k)
        {
            while (k > 1 && OrderHelper.Greater <Key>(pq[k / 2], pq[k]))
            {
                OrderHelper.Exch(pq, k, k / 2);
                k = k / 2;
            }
        }
Ejemplo n.º 2
0
 private void sink(int k)
 {
     while (2 * k <= N)
     {
         int j = 2 * k;
         if (j < N && OrderHelper.Greater <Key>(pq[j], pq[j + 1]))
         {
             j++;
         }
         if (!OrderHelper.Greater <Key>(pq[k], pq[j]))
         {
             break;
         }
         OrderHelper.Exch(pq, k, j);
         k = j;
     }
 }
Ejemplo n.º 3
0
        // is subtree of pq[1..N] rooted at k a min heap?
        private bool isMinHeap(int k)
        {
            if (k > N)
            {
                return(true);
            }
            int left = 2 * k, right = 2 * k + 1;

            if (left <= N && OrderHelper.Greater <Key>(pq[k], pq[left]))
            {
                return(false);
            }
            if (right <= N && OrderHelper.Greater <Key>(pq[k], pq[right]))
            {
                return(false);
            }
            return(isMinHeap(left) && isMinHeap(right));
        }