Adds a new int to this priority queue. @param x the new int to add to this priority queue
(int x)
| 84 | * @param x the new int to add to this priority queue |
| 85 | */ |
| 86 | public void insert(int x) { |
| 87 | // double size of array if necessary |
| 88 | if (n == pq.length - 1) resize(2 * pq.length); |
| 89 | |
| 90 | // add x, and percolate it up to maintain heap invariant |
| 91 | pq[++n] = x; |
| 92 | swim(n); |
| 93 | // assert isMaxHeap(); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Removes and returns a largest int on this priority queue. |