Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void extendHeap(int[] A, int i){
- int size = A.length;
- int max = i;
- int left = 2 * i;
- int right = 2 * i + 1;
- if (left < size && A[left] > A[max])
- max = left;
- if (right < size && A[right] > A[max])
- max = right;
- if (max != i) {
- int temp = A[i];
- A[i] = A[max];
- A[max] = temp;
- extendHeap(A, max);
- }
- }
- /**
- * Input:
- * array A of integers
- * Expected output:
- * the array A is now a max-heap under the tree view;
- */
- public static void buildHeapTopDown(int[] A){
- int size = A.length;
- int index = (size / 2);
- for (int i = index; i >= 0; i--) {
- extendHeap(A, i);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement