我正在尝试使用以下代码在 java 中创建一个 Max-Heap:
public class Heapify {
// 16 14 10 8 7 9 3 2 4 1
public static int[] Arr = {4, 1, 3, 2, 16, 9, 10, 14, 8, 7};
public static int counter = 0;
public static void main(String[] args) {
int kk;
for (kk = 0; kk <= Arr.length - 1; kk++) {
heapM(Arr, kk);
}
for (int krk = 0; krk < Arr.length; krk++) {
System.out.println(Arr[krk]);
}
}
public static void heapM(int[] Arr, int i) {
int largest;
int left = i * 2;
int right = i * 2 + 1;
if (((left < Arr.length) && (Arr[left] > Arr[i]))) {
largest = left;
} else {
largest = i;
}
if (((right < Arr.length) && (Arr[right] > Arr[largest]))) {
largest = right;
}
if (largest != i) {
swap(i, largest);
heapM(Arr, largest);
}
}
private static void swap(int i, int largest) {
int t = Arr[i];
Arr[i] = Arr[largest];
Arr[largest] = t;
}
}
所需的输出应该是:
16 14 10 8 7 9 3 2 4 1
而我得到
4 3 16 14 8 9 10 2 1 7
有人可以帮忙解释为什么堆没有正确构建吗?
谢谢