/**
* use maximum-top heap to implement priority queue
* define a MAX_SIZE_OF_PRIORITY_QUEUE to limit the max length of priority queue
* use a integer array to store element
* hypothesis i is the root node and then use 2*i to mark left child and 2*i+1 to mark right child
* use initialArray[0] to store the length of heap
* */
public class PriorityQueue{
private static final int MAX_SIZE_OF_PRIORITY_QUEUE=100;
private int[] initialArray;
/**
* initial priority queue with a exist array which can't be null
* */
public PriorityQueue(int[] initialElement) {
if(initialElement==null)return;
if(initialElement.length==0)return;
initialArray=new int[MAX_SIZE_OF_PRIORITY_QUEUE];
initialArray[0]=initialElement.length;
for(int i=0;i<initialElement.length;i++)initialArray[i+1]=initialElement[i];
System.out.println(initialArray[0]);
for (int i = initialArray[0]; i >0 ; i--)reBuildHeap(i);
}
/**
* rebuild array according to the value of each node
* maximum-top heap
* index represents the index of a node which should be rebuild(include it's children node)
*
* simple:
* 1
* 2 3
* 4 5 6 7
*
* */
private void reBuildHeap(int index){
System.out.println("execute rebuild function to rebuild a maximum-top heap with one loop");
int leftChildIndex=index*2;
int rightChildIndex=leftChildIndex+1;
int length=initialArray[0];
int biggerValueIndex=-1;
if(leftChildIndex>length&&rightChildIndex>length){
System.out.println("no left child");
return;
}
if(leftChildIndex<=length&&rightChildIndex>length){
System.out.println("only left child");
biggerValueIndex=leftChildIndex;
}
if(leftChildIndex>length&&rightChildIndex<=length){
System.out.println("only right child");
biggerValueIndex=rightChildIndex;
}
if(leftChildIndex<=length&&rightChildIndex<=length){
System.out.println("both children");
biggerValueIndex=initialArray[leftChildIndex]>initialArray[rightChildIndex]?leftChildIndex:rightChildIndex;
}
if(initialArray[index]>initialArray[biggerValueIndex]){
System.out.println("unnecessary to swap!");
return;
}else{
int temp=initialArray[index];
initialArray[index]=initialArray[biggerValueIndex];
initialArray[biggerValueIndex]=temp;
this.reBuildHeap(biggerValueIndex);
}
}
public int getLength() {
return initialArray[0];
}
/**
* get top priority value of heap
* the first element of array
* */
public int priority(){
return initialArray[1];
}
/**
* length++
* add element to the tail of array
* rebuild the heap to regular priority heap
* */
public void insert(int element){
initialArray[0]++;
initialArray[initialArray[0]]=element;
for(int i=initialArray[0];i>=1;i=i/2){
reBuildHeap(i);
}
}
/**
* length--
* swap the first element and last element
* delete last value
* rebuild the heap
* */
public int deletePriority(){
if(initialArray[0]<=0)return -1;
int maxValue=initialArray[1];
initialArray[1]=initialArray[initialArray[0]];
initialArray[0]--;
for(int i=initialArray[0];i>=1;i=i/2){
reBuildHeap(i);
}
return maxValue;
}
/**
* print the structure of priority heap
* */
@Override
public String toString() {
StringBuilder builder=new StringBuilder("{");
for (int i = 1; i <= initialArray[0]; i++) {
if(i!=1)builder.append(",");
builder.append(initialArray[i]);
}
builder.append("}");
return builder.toString();
}
}
PriorityQueue(java)
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- org.springframework.beans.factory.UnsatisfiedDependencyEx...
- 1.有很多人是通过自学,比如买些专业的java书籍、或者通过网上免费视频学习、进入相应的java论坛等等。ja...
- 如果一个人在朋友圈,用极其粗俗肮脏的语言将一个女孩子骂得体无完肤,你会怎么想? 大部分人的第一想法就是这个女孩肯定...