数据结构-堆实现优先队列(java)
生活随笔
收集整理的這篇文章主要介紹了
数据结构-堆实现优先队列(java)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
隊列的特點是先進先出。通常都把隊列比喻成排隊買東西,大家都很守秩序,先排隊的人就先買東西。但是優先隊列有所不同,它不遵循先進先出的規則,而是根據隊列中元素的優先權,優先權最大的先被取出。這就很像堆的特征:總是移除優先級最高的根節點。
重點:優先級隊列,是要看優先級的,誰的優先級更高,誰就先得到權限。不分排隊的順序!
上篇文章解釋了堆的概念實現,現在用堆實現優先隊列:
//最大堆 import java.util.ArrayList; public class Heap<E extends Comparable>{private ArrayList<E> list=new ArrayList<E>();//用數組實現堆public Heap(){}public Heap(E[] objects){for(int i=0;i<objects.length;i++){add(objects[i]);}}public void add(E newObject){//添加一個元素list.add(newObject);int currentIndex=list.size()-1;while(currentIndex>0){int parentIndex=(currentIndex-1)/2;//找到該結點的父結點if(list.get(currentIndex).compareTo(list.get(parentIndex))>0){//與父節點比較//如果當前結點的值大于父結點就交換位置E temp=list.get(currentIndex);list.set(currentIndex, list.get(parentIndex));list.set(parentIndex, temp); }elsebreak;currentIndex=parentIndex;} }public E remove(){//刪除并返回根結點,堆的特點是移除了根結點后還是堆if(list.size()==0) return null;E removeObject=list.get(0);list.set(0, list.get(list.size()-1));//把最后一個結點放在根結點的位置list.remove(list.size()-1);int currentIndex=0;while(currentIndex<list.size()){int leftChildIndex=2*currentIndex+1;int rightChildIndex=2*currentIndex+2;//左右孩子結點的坐標if(leftChildIndex>=list.size())break;//比較左右孩子的值,使maxIndex指向值大的結點int maxIndex=leftChildIndex;if(rightChildIndex<list.size()){if(list.get(maxIndex).compareTo(list.get(rightChildIndex))<0){maxIndex=rightChildIndex;}}//如果當前結點的值小于其左右孩子中的大的值,就交換兩個結點if(list.get(currentIndex).compareTo(list.get(maxIndex))<0){E temp=list.get(maxIndex);list.set(maxIndex, list.get(currentIndex));list.set(currentIndex, temp);currentIndex=maxIndex;}elsebreak;}return removeObject; }public int getSize(){return list.size();}}MyPriorityQueue.java
public class MyPriorityQueue<E extends Comparable> {private Heap<E> heap=new Heap<E>();//用堆實現優先隊列//入隊列public void enqueue(E e){heap.add(e); //這個add以后,堆會自己調整成一個新堆}//出隊列public E dequeue(){return heap.remove();//這移除出之后,堆會自己調整,還是一個新堆}public int getSize(){return heap.getSize();}} TestMyPriorityQueueMainClass.java
public class TestMyPriorityQueueMainClass {public static void main(String[] args) {// TODO Auto-generated method stubPatient p1=new Patient("John",2);Patient p2=new Patient("Tom",9);Patient p3=new Patient("Jack",4);Patient p4=new Patient("Michael",6);MyPriorityQueue<Patient> priorityQueue=new MyPriorityQueue<>();priorityQueue.enqueue(p1);priorityQueue.enqueue(p2);priorityQueue.enqueue(p3);priorityQueue.enqueue(p4);while(priorityQueue.getSize()>0){System.out.print(priorityQueue.dequeue()+" ");}}static class Patient implements Comparable{private String name;private int priority;public Patient(String name,int priority){this.name=name;this.priority=priority;}public String toString(){return name+"(priority:"+priority+")";}@Overridepublic int compareTo(Object oo) {//比較優先級// TODO Auto-generated method stub return this.priority-((Patient)oo).priority;}} }
測試結果: 優先級高的先輸出,優先級最高的就是堆的根節點
總結
以上是生活随笔為你收集整理的数据结构-堆实现优先队列(java)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据结构-堆的实现
- 下一篇: 数据结构-二叉树和二叉查找树