用堆实现优先级队列,O(logn)
1、完全二叉树。除了最后一层节点不需要是满的,其他的每一层从左到右都完全是满的
2、常由数组实现。
3、堆里的每一个节点都满足:父节点关键字大于所有的子节点。
public class Node {
private int data;
public Node(int key){
data=key;
}
public int getKey(){
return data;
}
public void setKey(int id){
data=id;
}
}
public class Heap {
private Node[] heapArray;
private int size;
private int currentsize;
public Heap(int n){
size=n;
currentsize=0;
heapArray=new Node[size];
}
public boolean isEmpty(){
return size==0;
}
public boolean insert(int key){
if(currentsize==size) return false;
Node newNode=new Node(key);
heapArray[currentsize]=newNode;
trickUp(currentsize);//新节点放在最后,比较之后向上调整
currentsize++;
return true;
}
public void trickUp(int index){
int parent=(index-1)/2;
Node bottom=heapArray[index];
while(index>0 && heapArray[parent].getKey()<bottom.getKey()){
heapArray[index]=heapArray[parent];
index=parent;
parent=(parent-1)/2;
}
heapArray[index]=bottom;
}
public Node delete(){ // 删除最大项
Node root=heapArray[0];
heapArray[0]=heapArray[--currentsize];//最后的数据项移动到根上
trickDown(0);
return root;
}
public void trickDown(int index){
int lagerChild;
Node top=heapArray[index];
while(index<currentsize/2){ //如果未到最后一层
int leftChild=2*index+1;
int rightChild=leftChild+1;
if(rightChild<currentsize && heapArray[leftChild].getKey()<heapArray[rightChild].getKey())
lagerChild=rightChild;
else
lagerChild=leftChild;
if(top.getKey()>=heapArray[lagerChild].getKey()) break;
heapArray[index]=heapArray[lagerChild];
index=lagerChild;
}
heapArray[index]=top;
}
public boolean change(int index,int newValue){
if(index<0 || index>= currentsize){
return false;
}
int oldValue=heapArray[index].getKey();
heapArray[index].setKey(newValue);
if(oldValue<newValue) trickUp(index);
else trickDown(index);
return true;
}
public void displayHeap(){
System.out.print("heap:");
for(int i=0;i<currentsize;i++){
if(heapArray[i]!=null) System.out.print(heapArray[i].getKey()+" ");
else System.out.print("-- ");
}
//树状输出
int nBlank=32;
int itemsPerRow=1;
int column=0;
int j=0;
String dots="........";
System.out.println(dots+dots);
while(currentsize>0){
if(column==0){
for(int k=0;k<nBlank;k++)
System.out.print(" ");
}
System.out.print(heapArray[j].getKey());
if(++j==currentsize) break; //打印完了
if(++column==itemsPerRow) { //一行打印完了
nBlank/=2;
itemsPerRow*=2;
column=0;
System.out.println(); //打印完换行
}else
for(int k=0;k<nBlank*2-2;k++) System.out.print(" ");
}
System.out.println(dots+dots);
}
public void deleteKey(int n){
int i;
for(i=0;i<currentsize;i++){
if(heapArray[i].getKey()==n){
heapArray[i]=heapArray[--currentsize];
trickDown(i);
break;
}
}
//System.out.print(i);
}
}
import java.io.*;
public class heapApp {
public static String getString() throws IOException{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
return br.readLine();
}
public static char getchar() throws IOException{
return getString().charAt(0);
}
public static int getInt() throws IOException{
return Integer.parseInt(getString());
}
public static void main(String[] args){
Heap theheap=new Heap(31);
theheap.insert(70);
theheap.insert(40);
theheap.insert(50);
theheap.insert(20);
theheap.insert(60);
theheap.insert(100);
theheap.insert(80);
theheap.insert(30);
theheap.insert(10);
theheap.insert(90);
theheap.displayHeap();
//theheap.delete();
theheap.deleteKey(90);
theheap.displayHeap();
}
}