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 siftDown(int nodeIndex) { int leftChildIndex = getLeftChildIndex(nodeIndex); if (leftChildIndex >= heapSize) return; int minIndex = -1; double minCosts = Double.POSITIVE_INFINITY; int limitChildIndex = Math.min(leftChildIndex + fanout, heapSize + 1); for (int childIndex = leftChildIndex; childIndex < limitChildIndex; childIndex++) { /* * If the costs are equal, use the array indices to define the sort order. * Doing so should guarantee a deterministic order of the heap entries. */ double childCosts = costs[childIndex]; if (childCosts <= minCosts) { if (childCosts < minCosts || this.getIndex(data[childIndex]) < this.getIndex(data[minIndex])) { minIndex = childIndex; minCosts = childCosts; } } } if (minIndex >= 0) { swapData(nodeIndex, minIndex); siftDown(minIndex); } }