Data structure-Heap Implementation Priority Queue (Java)

Source: Internet
Author: User
Tags comparable

The queue is characterized by FIFO. Usually the queue is likened to the line to buy things, everyone is very orderly, first line people first buy things. but the priority queue is different, it does not follow the FIFO rule, but based on the priority of the elements in the queue, priority is first taken out. This is like a heap feature: Always remove the highest-priority root node.

Focus: Priority queue, is to see priority, who priority is higher, who will get permissions first. Not in the order of queuing!

The previous article explained the concept implementation of the heap and now implements the priority queue with the heap:


Max Heap Import Java.util.arraylist;public class Heap<e extends Comparable>{private arraylist<e> list=new    Arraylist<e> ();//implement Heap public heap with an array () {} public heap (e[] objects) {for (int i=0;i<objects.length;i++) {    Add (Objects[i]);    }} public void Add (E newObject) {//Add an element List.add (NewObject);        int Currentindex=list.size ()-1; while (currentindex>0) {int parentindex= (currentIndex-1)/2;//Find the parent node of the node if (List.get (currentindex). CompareTo (    List.get (Parentindex)) >0) {//Compare with parent node//If the value of the current node is greater than the parent junction, swap position E temp=list.get (currentindex);    List.set (Currentindex, List.get (Parentindex));       List.set (Parentindex, temp);    } else break;    Currentindex=parentindex;        }} public E remove () {//delete and return the root node, the heap is characterized by removing the root node or the heap if (List.size () ==0) return null;    E removeobject=list.get (0);        List.set (0, List.get (list.size ()-1));//Place the last node at the root node List.remove (list.size ()-1);    int currentindex=0; while (currentindex<lIst.size ()) {int leftchildindex=2*currentindex+1;    int rightchildindex=2*currentindex+2;//around the child node coordinates if (Leftchildindex>=list.size ()) break;     Compare the value of the left and right child, so that maxindex points to a large value of the node int maxindex=leftchildindex; if (Rightchildindex<list.size ()) {if (List.get (Maxindex). CompareTo (List.get (Rightchildindex)) <0) {maxindex=     Rightchildindex;              }}//If the value of the current node is less than the large value of its left and right children, swap two nodes 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;    } else break;           } return removeobject;    } public int GetSize () {return list.size (); }    }
Mypriorityqueue.java

public class Mypriorityqueue<e extends comparable> {       private heap<e> heap=new heap<e> ();// Use heap to implement priority queue     //into queue public       void Enqueue (e e) {       heap.add (e);//After this add, the heap adjusts itself to a new heap       }       //out queue       public E dequeue () {       return heap.remove ();//When this is removed, the heap adjusts itself or a new heap} public       int getsize () {       return Heap.getsize ();       }      }
Testmypriorityqueuemainclass.java

public class Testmypriorityqueuemainclass {public static void main (string[] args) {//TODO auto-generated method stub        Patient 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;         The public String, toString () {return name+ "(Priority:" +priority+ ")"; } @Overridepublic int CompareTo (Object oo) {//Comparison priority//TODO AutO-generated Method Stubreturn this.priority-((Patient) OO). Priority;} }}

Test results:high priority first output, highest priority is the root node of the heap








Data structure-Heap Implementation Priority Queue (Java)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.