Data Structure-heap implementation priority queue (java)
The queue features FIFO. Generally, queues are similar to queuing for shopping. Everyone is very orderly. the first person in the queue buys something first. The priority queue is different. It does not follow the first-in-first-out rule, but is extracted first with the highest priority based on the priority of the elements in the queue. This is similar to the heap feature: the root node with the highest priority is always removed.
Key: priority queue, which depends on the priority. Whoever has a higher priority gets the permission first. No queuing order!
The previous article explains the implementation of the concept of heap, And now uses heap to implement priority queue:
// Maximum Heap import java. util. ArrayList; public class Heap
{Private ArrayList
List = new ArrayList
(); // Use an array to implement the public Heap () {} public Heap (E [] objects) {for (int I = 0; I
0) {int parentIndex = (currentIndex-1)/2; // find the node's parent node if (list. get (currentIndex ). compareTo (list. get (parentIndex)> 0) {// compare with the parent node // if the value of the current node is greater than that of the parent node, the switch location 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 feature is whether to remove the root node or heap if (list. size () = 0) return null; E removeObject = list. get (0); list. set (0, list. get (list. size ()-1); // place the last node in the root node list. remove (list. size ()-1); int currentIndex = 0; while (currentIndex
= List. size () break; // compare the left and right values of the child so that maxIndex points to the node with a large value int maxIndex = leftChildIndex; if (rightChildIndex
MyPriorityQueue. java
Public class MyPriorityQueue
{Private Heap
Heap = new Heap
(); // Use heap to implement priority queue // public void enqueue (E e) {heap. add (e); // after this add, the heap will automatically change to a new heap} // public E dequeue () {return heap. remove (); // after this operation is removed, the heap will adjust 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
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) {// compare priority // TODO Auto-generated method stubreturn this. priority-(Patient) oo ). priority ;}}}
Test results: the first output with the highest priority is the heap root node.