1. Overview
A queue is a data structure that satisfies the requirements of first-in-first-out (FIFO). Data is taken from the queue header, and new data is inserted from the end of the queue. Data is equal and has no priority. This is similar to the process where ordinary people arrive at the railway station to buy tickets in a queue. First come, they buy tickets first. Each person is equal and there is no priority right. The whole process is fixed. The priority queue can be understood as assigning a weight to each data on the basis of the queue, representing the data priority. Like a queue, a priority queue also extracts data from the header and inserts data from the tail. However, this process changes based on the data priority and always comes out first with a higher priority, therefore, it is not necessarily FIFO. This is similar to buying a train ticket when a military person gives priority to the ordinary person. Although a military person comes late, the priority of a military person is higher than that of an ordinary person, and he can always buy a ticket first. Generally, Priority Queues are used in multi-task scheduling in the operating system. The higher the task priority, the higher the task priority (similar to the out-of-queue). If the priority of a later task is higher than the previous one, you need to adjust the task to a proper position for priority execution. The whole process always gives the highest priority to the first task in the queue.
There are two types of Priority Queues: the maximum priority queue and the minimum priority queue. The two types can be implemented using the maximum and minimum heap respectively. The book introduces the maximum priority queue based on the maximum heap. The maximum priority queue supports the following operations:
Insert (S, X): insert element X to the set S
Maximum (s): returns the element with the largest keyword in S.
Extract_max (s): removes and returns the element with the maximum keyword in S.
Increase_key (S, X, k): Increase the key value of element x to K. Here, the K value cannot be less than the value of the original key word of X.
2. maximum priority queue operation implementation
(1) heap_maximum uses O (1) Time to implement maximum (s) operations, that is, to return the value of the first element of the maximum heap (return a [1]).
(2) heap_extract_max implements the extract_max operation, deletes the first element in the maximum heap, and then adjusts the heap. The procedure is as follows: copy the last element in the most heap to the first position, delete the last node (reduce the heap size by 1), and adjust the heap from the first node position, makes it the new Max heap. The procedure is as follows:
The pseudocode is described as follows:
1 HEAD_EXTRACT_MAX(A)2 if heap_size[A]<13 ther error4 max = A[1]5 A[1] = A[heap_size[A]];6 heap_size[A] = heap_size[A]-17 adjust_max_heap(A,1)8 return MAX
(3) heap_increase_key implements increase_key, and uses subscript to identify the priority key of the element to be added. After adding the element, you need to adjust the heap. From the parent node of the node, the node starts to adjust from top to top. The procedure is as follows:
The pseudocode is described as follows:
1 HEAP_INCREASE_KEY(A,i,key)2 if key < A[i]3 then error4 A[i] = key5 while i>1 && A[PARENT(i)] <A[i]6 do exchange A[i] <-> A[PARENT(i)]7 i = PARENT(i)
(4) max_heap_insert is used to insert a new keyword into the maximum heap. The new keyword is inserted at the end of the priority team, and then adjusted from the top to the top of the parent node at the end of the stack. The pseudocode description is as follows:
1 MAX_HEAP_INSERT(A,key)2 heap_size[A] = heap_size[A]+13 A[heap_size[A]] = -0;4 HEAP_INCREASE_KEY(A,heap_size[A],key)
Chapter 6 priority queue