package lee.tools;
public class MinHeap {
public int[] arr;
int size;
public int end;
public MinHeap(){
size = 5;
end = -1;
arr = new int[size];
}
public boolean isFull(){
if(size-1==end){
return true;
}
else{
return false;
}
}
public void enLarge(){
size = size*2;
int[] newArr = new int[size];
for(int i=0;i
newArr[i] = arr[i];
}
arr = newArr;
}
public boolean isEmpty(){
if(end==-1){
return true;
}
else{
return false;
}
}
public void siftUp(int n){
for(int i=n;i>0; i=(i-1)/2){
int father = (i-1)/2;
if(arr[i] < arr[father]){
int temp = arr[i];
arr[i] = arr[father];
arr[father] = temp;
}
else{
return;
}
}
}
public void siftDown(int n){
for(int i = n*2+1;i<=end;i=i*2+1){
if(arr[i+1]
i++;
}
int father = (i-1)/2;
if(arr[i] < arr[father]){
int temp = arr[i];
arr[i] = arr[father];
arr[father] = temp;
}
else{
return;
}
}
}
public void insert(int data){
if(isFull()){
enLarge();
}
arr[++end] = data;
siftUp(end);
}
public int outTop(){
if(isEmpty()){
System.out.println("The heap is empty!!");
return -9999;
}
int temp = arr[0];
arr[0] = arr[end--];
siftDown(0);
return temp;
}
}