public Object removeMin() throws PriorityQueueEmptyException { if (isEmpty()) throw new PriorityQueueEmptyException("Empty priority queue!"); Object min = element(T.root()); if (size() == 1) T.remove(); else { T.replaceElement(T.root(), T.remove()); Position r = T.root(); while (T.isInternal(T.leftChild(r))) { // down-heap bubbling Position s; if (T.isExternal(T.rightChild(r)) || comp.isLessThanOrEqualTo(key(T.leftChild(r)), key(T.rightChild(r)))) s = T.leftChild(r); else s = T.rightChild(r); if (comp.isLessThan(key(s), key(r))) { T.swapElements(r, s); r = s; } else break; } } return min; }