向下调整
public class Heap {
public static void shiftdown(int[] array,int size,int index){
int parent=index;
int child=2parent+1;
while(child<size){
if(child+1 < size && array[child+1] > array[child]){
child=child+1;
}
if(array[child]>array[parent]){
int tmp=array[child];
array[child]=array[parent];
array[parent]=tmp;
}else{
break;
}
parent=child;
child=2parent+1;
}
}
向上调整
public static void shiftUp(int[] array, int index) {
int child = index;
int parent = (child - 1) / 2;
while (child > 0) {
if (array[parent] < array[child]) {
int tmp = array[parent];
array[parent] = array[child];
array[child] = tmp;
} else {
break;
}
child = parent;
parent = (child - 1) / 2;
}
}
优先级队列
最新推荐文章于 2024-07-24 12:40:06 发布