ackage PriorityQueue;
import java.util.Arrays;
/**
* Created with IntelliJ IDEA.
* Description:
* User: czt20
* Date: 2026 -01-15
* Time: 21:06
*/
public class PriorityQueue {
public int[] elm;
public int usdesize = 0;
public PriorityQueue() {
this.elm = new int[10];
}
public void input(int[] arr) {
for (int i = 0; i < arr.length; i++) {
elm[i] = arr[i];
this.usdesize++;
}
}
public void creatHeap() {
for (int p = (this.usdesize - 1 - 1) / 2; p >= 0; p--) {
shiftDown(p, this.usdesize);
}
}
//大根堆
private void shiftDown(int p, int usdesize) {
int child = 2 * p + 1;
while (child < usdesize) {
if (child + 1 < usdesize && elm[child] < elm[child + 1]) {
child++;
}
if (elm[child] > elm[p]) {
int temp = elm[child];
elm[child] = elm[p];
elm[p] = temp;
p = child;
child = 2 * p + 1;
} else {
break;
}
}
}
public void h() {
for (int i = 0; i < elm.length; i++) {
if (elm[i]!=0){
System.out.println(elm[i]);
}
}
}
public void push(int val) {
if (isFull()) {
elm= Arrays.copyOf(elm,2*elm.length);
}
elm[usdesize] = val;
shiftup(usdesize);
usdesize++;
}
private boolean isFull() {
return usdesize== elm.length;
}
//小根堆
private void shiftup(int child) {
int parent = (child - 1) / 2;
while (parent >= 0) {
if (elm[child] < elm[child + 1]) {
child++;
}
if (elm[child] > elm[parent]) {
int temp = elm[child];
elm[child] = elm[parent];
elm[parent] = temp;
child = parent;
parent = (child - 1) / 2;
} else {
break;
}
}
}
//插入
public int poll(int index){
if(index>usdesize-1){
return -1;
}
int val=elm[index];
int temp=elm[index];
elm[index]=elm[usdesize-1];
elm[usdesize-1]=temp;
shiftDown(0,usdesize-1);
usdesize--;
return elm[index];
}
public int findMaxki(){
}
}