我知道对于一个堆是最大有序堆,父节点的密钥应该大于或等于子节点的密钥,最高密钥应该在根节点中。我只是想确定我想的方式是否正确 -
如果我想检查给定数组的可比较对象是否是最大有序堆
boolean isItMaxOrdered(Comparable[] a) {
//Check for 2 * i where i is the index of the first element
//Check for 2 * i + 1 element where i is the index of the first element
//Compare if element i is greater than 2 * i and also if (2 * i) is greater than (2 * i + 1)th element
boolean check = false;
for(int i = 0; i < a.length; i++) {
if((2 * i + 1) < a.length)
if(a[i].compareTo(a[2 * i]) > 0 && a[i].compareTo(a[2 * i + 1]) > 0 && a[2 * i].compareTo(a[2 * i + 1]) > 0)
check = true;
else
check = false;
}
return check;
}