Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. public static void extendHeap(int[] A, int i){
  2. int size = A.length;
  3. int max = i;
  4. int left = 2 * i;
  5. int right = 2 * i + 1;
  6. if (left < size && A[left] > A[max])
  7. max = left;
  8.  
  9. if (right < size && A[right] > A[max])
  10. max = right;
  11.  
  12. if (max != i) {
  13. int temp = A[i];
  14. A[i] = A[max];
  15. A[max] = temp;
  16.  
  17. extendHeap(A, max);
  18. }
  19.  
  20. }
  21.  
  22. /**
  23. * Input:
  24. * array A of integers
  25. * Expected output:
  26. * the array A is now a max-heap under the tree view;
  27. */
  28. public static void buildHeapTopDown(int[] A){
  29. int size = A.length;
  30. int index = (size / 2);
  31.  
  32.  
  33. for (int i = index; i >= 0; i--) {
  34. extendHeap(A, i);
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement