I will do it. Here is a way to do what you asked for, with some explanation.
, - , , , , . , "", . for:
public void rebuildHeap()
{
int half = heapArray.length / 2;
for(int i = half; i >= 0; i--)
restoreHeap(i);
}
restoreHeap?
node index , , node. , index node , index node . , .
. , , :
private void restoreHeap(int index)
{
int leftChild = (index * 2) + 1;
int rightChild = leftChild +1;
...
childrens index. , index node node. , ( ). , , , index node .
...
int biggest = index;
if(leftChild < currentSize && heapArray[leftChild].getKey() > heapArray[index].getKey())
biggest = leftChild; //LeftChild is bigger
if(rightChild < currentSize && heapArray[rightChild].getKey() > heapArray[biggest].getKey())
biggest = rightChild; //RightChild is bigger than both leftChild and the index node
if(biggest != index) //If a swap is needed
{
//Swap
Node swapper = heapArray[biggest];
heapArray[biggest] = heapArray[index];
heapArray[index] = swapper;
restoreHeap(biggest);
}
}