/** * insertWithOverflow() is the same as insert() except its * return value: it returns the object (if any) that was * dropped off the heap because it was full. This can be * the given parameter (in case it is smaller than the * full heap's minimum, and couldn't be added), or another * object that was previously the smallest value in the * heap and now has been replaced by a larger one, or null * if the queue wasn't yet full with maxSize elements. */ public Object insertWithOverflow(Object element) { if (size < maxSize) { put(element); return null; } else if (size > 0 && !lessThan(element, heap[1])) { Object ret = heap[1]; heap[1] = element; adjustTop(); return ret; } else { return element; } }
/** * insertWithOverflow() is the same as insert() except its * return value: it returns the object (if any) that was * dropped off the heap because it was full. This can be * the given parameter (in case it is smaller than the * full heap's minimum, and couldn't be added), or another * object that was previously the smallest value in the * heap and now has been replaced by a larger one, or null * if the queue wasn't yet full with maxSize elements. */ public Object insertWithOverflow(Object element) { if (size < maxSize) { put(element); return null; } else if (size > 0 && !lessThan(element, heap[1])) { Object ret = heap[1]; heap[1] = element; adjustTop(); return ret; } else { return element; } }
/** * Adds element to the PriorityQueue in log(size) time if either * the PriorityQueue is not full, or not lessThan(element, top()). * @param element * @return true if element is added, false otherwise. */ public boolean insert(Object element){ if(size < maxSize){ put(element); return true; } else if(size > 0 && !lessThan(element, top())){ heap[1] = element; adjustTop(); return true; } else return false; }