()
| 39 | } |
| 40 | |
| 41 | public int delMax() { |
| 42 | if (isEmpty()) |
| 43 | return MAGIC_NUMBER; |
| 44 | int max = pq[1]; |
| 45 | exch(1, n--); |
| 46 | sink(1); |
| 47 | pq[n + 1] = MAGIC_NUMBER; // to avoid loitering and help with garbage collection |
| 48 | if ((n > 0) && (n == (pq.length - 1) / 4)) |
| 49 | resize(pq.length / 2); |
| 50 | return max; |
| 51 | } |
| 52 | |
| 53 | private void swim(int k) { |
| 54 | while (k > 1 && less(k / 2, k)) { |