2019年的第一篇文章,作为一个程序员,从18年coding到了19年,真是太开心啦
/**
* based on min-heap
* @author haofan.whf
* @version $Id: PriorityQueue.java, v 0.1 2018年12月31日 23:23 haofan.whf Exp $
*/
public class PriorityQueue {
private int[] heap;
private int lastEleIndex = 0;
public PriorityQueue(int heapSize){
this.heap = new int[heapSize + 1];
this.heap[0] = Integer.MIN_VALUE;
}
public int length(){
return this.heap.length;
}
public void append(int ele){
this.lastEleIndex ++;
if(lastEleIndex > this.length() - 1){
throw new RuntimeException("too much elements");
}
this.heap[lastEleIndex] = ele;
for (int i = lastEleIndex; i > 1; ) {
int parentIndex = i / 2;
if(this.heap[i] < this.heap[parentIndex]){
this.swap(i, parentIndex);
}
i = parentIndex;
}
}
public int pop(){
if(this.heap.length == 1){
throw new RuntimeException("heap is empty");
}
int popedKey = this.heap[1];
this.heap[1] = this.heap[lastEleIndex];
this.heap[lastEleIndex] = 0;
lastEleIndex --;
if(this.heap.length == 2){
return popedKey;
}
for (int i = 1; i <= lastEleIndex / 2;) {
int leftChildIndex = 2 * i;
int rightChildIndex = 2 * i + 1;
if(rightChildIndex > lastEleIndex){
//means i only has left child
if(this.heap[i] > this.heap[leftChildIndex]){
this.swap(i, leftChildIndex);
i = leftChildIndex;
}else{
break;
}
}else{
//means i only has left child and right child
if(this.heap[leftChildIndex] > this.heap[rightChildIndex]){
if(this.heap[i] > this.heap[rightChildIndex]){
this.swap(i, rightChildIndex);
i = rightChildIndex;
}else{
break;
}
}else{
if(this.heap[i] > this.heap[leftChildIndex]){
this.swap(i, leftChildIndex);
i = leftChildIndex;
}else{
break;
}
}
}
}
return popedKey;
}
//swap i and j
private void swap(int i, int j){
int temp = this.heap[i];
this.heap[i] = this.heap[j];
this.heap[j] = temp;
}
public int findMin(){
if(this.heap.length == 1){
throw new RuntimeException("heap is empty");
}
return heap[1];
}
public void checkRI(){
if(this.heap.length <= 2){
return;
}
for (int i = 1; i <= this.lastEleIndex / 2; i++) {
if(heap[i] > heap[2 * i]){
throw new RuntimeException("it's not a min-heap");
}
if(i * 2 + 1 <= this.lastEleIndex && heap[i] > heap[i * 2 + 1]){
throw new RuntimeException("it's not a min-heap");
}
}
}
public String toString(){
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= this.lastEleIndex; i++) {
sb.append(this.heap[i] + ",");
}
return sb.toString();
}
public static void main(String args[]){
PriorityQueue priorityQueue = new PriorityQueue(10);
priorityQueue.append(3);
priorityQueue.append(2);
priorityQueue.append(1);
priorityQueue.append(10);
priorityQueue.append(4);
priorityQueue.append(7);
priorityQueue.append(8);
priorityQueue.append(6);
priorityQueue.checkRI();
System.out.println(priorityQueue);
Assert.assertEquals(priorityQueue.findMin(), 1);
Assert.assertEquals(priorityQueue.findMin(), 1);
Assert.assertEquals(priorityQueue.pop(), 1);
Assert.assertEquals(priorityQueue.pop(), 2);
Assert.assertEquals(priorityQueue.pop(), 3);
System.out.println(priorityQueue);
priorityQueue.checkRI();
}
}