private void percolateDown(int index) { int largerChild; BinaryHeapNode top = array[index]; while (index < currentSize / 2) { int leftChild = 2 * index + 1; int rightChild = leftChild + 1; if (rightChild < currentSize && array[leftChild].getValue() > array[rightChild].getValue()) { largerChild = rightChild; } else { largerChild = leftChild; } if (top.getValue() <= array[largerChild].getValue()) { break; } array[index] = array[largerChild]; index = largerChild; } array[index] = top; }
public int remove() { BinaryHeapNode root = array[0]; array[0] = array[--currentSize]; percolateDown(0); return(root.getValue()); }
public bool add(int value) { if (currentSize == maxSize) { return(false); } array[currentSize] = new BinaryHeapNode(value); percolateUp(currentSize++); return(true); }
public void percolateUp(int index) { int parent = (index - 1) / 2; BinaryHeapNode bottom = array[index]; while (index > 0 && array[parent].getValue() > bottom.getValue()) { array[index] = array[parent]; index = parent; parent = (parent - 1) / 2; } array[index] = bottom; }
public void addFreely(int value) { array[currentSize] = new BinaryHeapNode(value); currentSize++; }