12.07.2015 Views

slides - Unisinos

slides - Unisinos

slides - Unisinos

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Heap (5)public void heapfyDown(int index) {int largerChild;Node top = heapArray[index]; // save rootwhile (index < currentSize / 2) // while node has at least one child{int leftChild = 2 * index + 1;int rightChild = leftChild + 1;// find larger childif (rightChild < currentSize && // (rightChild exists?)heapArray[leftChild].getKey() < heapArray[rightChild].getKey())largerChild = rightChild;elselargerChild = leftChild;// top >= largerChild?if (top.getKey() >= heapArray[largerChild].getKey())break;// shift child upheapArray[index] = heapArray[largerChild];index = largerChild; // go down} // end whileheapArray[index] = top; // root to index} // end trickleDown()28

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!