private int removeSiftDown(int nodeIndex) { while(true) { int leftChildIndex = getLeftChildIndex(nodeIndex); if (leftChildIndex >= heapSize) break; double leftCosts = costs[leftChildIndex]; int limitChildIndex = Math.min(leftChildIndex + fanout, heapSize); for (int rightChildIndex = leftChildIndex + 1; rightChildIndex < limitChildIndex; rightChildIndex++) { /* * We use the sentinel values Double.MAX_VALUE to protect * ourselves from looking beyond the heap's true size */ double rightCosts = costs[rightChildIndex]; if (leftCosts >= rightCosts && (leftCosts > rightCosts || this.getIndex(data[leftChildIndex]) > this.getIndex(data[rightChildIndex]))) { leftChildIndex = rightChildIndex; leftCosts = rightCosts; } } copyData(nodeIndex, leftChildIndex); nodeIndex = leftChildIndex; } return nodeIndex; }
private void siftUp(int index, E newEntry, double newCost) { while (index > 0) { int parentIndex = getParentIndex(index); double parentCost = costs[parentIndex]; if (newCost > parentCost) break; if (newCost == parentCost && this.getIndex(newEntry) > this.getIndex(data[parentIndex])) break; this.copyData(index, parentIndex); // for next iteration index = parentIndex; } data[index] = newEntry; costs[index] = newCost; indices[this.getIndex(newEntry)] = index; }