示例#1
0
 private void sink(int k) {
   while (2 * k <= N) {
     int j = 2 * k;
     if (j < N && greater(j, j + 1)) j++;
     if (!greater(k, j)) break;
     exch(k, j);
     k = j;
   }
 }
示例#2
0
 /**
  * Removes and returns a smallest key on the priority queue.
  *
  * @return a smallest key on the priority queue
  * @throws java.util.NoSuchElementException if the priority queue is empty
  */
 public Key delMin() {
   if (isEmpty()) throw new NoSuchElementException("Priority queue underflow");
   exch(1, N);
   Key min = pq[N--];
   sink(1);
   pq[N + 1] = null; // avoid loitering and help with garbage collection
   if ((N > 0) && (N == (pq.length - 1) / 4)) resize(pq.length / 2);
   assert isMinHeap();
   return min;
 }
示例#3
0
 /**
  * ********************************************************************* Helper functions to
  * restore the heap invariant.
  * ********************************************************************
  */
 private void swim(int k) {
   while (k > 1 && greater(k / 2, k)) {
     exch(k, k / 2);
     k = k / 2;
   }
 }