[Queue] - PriorityQueue priority queue

queue

As we all know, the characteristics of queues are first-in, first-out (FIFO), just like queuing.

insert image description here

binary heap

Binary heap is a special kind of heap. There are two main applications. The first is a sorting method——heap sort, the second ispriority queue

There are two types of binary heaps: max heap and min heap.

  • Maximum heap: The top of the maximum heap is the largest element in the entire heap, and the key value of the parent node is always greater than or equal to the key value of any child node;
  • Min heap: The top of the min heap is the smallest element in the entire heap, and the key value of the parent node is always less than or equal to the key value of any child node.

A binary heap is actually a special kind of binary tree (complete binary tree, with nodes layer by layer, arranged in order from left to right), but stored in an array. In general linked list binary tree, we operate the pointer of the node, and in the array, we use the array index as the pointer.

When we represent a heap (large top heap or small top heap), we actually use aone-dimensional arrayTo maintain a D-ary heap, first look at the binary tree in the figure below (d=2, d means that each parent node has at most several child nodes), the number represents the index:

  • The index of the parent node of any node is: (index - 1) / d
  • The index of the left child of any node is: (index * d) + 1
  • The index of the right child of any node is: (index * d) + 2

Its time complexity is O(logn·dn).

There are two main operations of the binary heap, sink (sinking) and swim (floating).

Take the big top heap as an example

swim (float)

If a node A is larger than its parent node, then A should not be a child node, but should replace the parent node and be the parent node by itself, which is the floating of A.

sink (sink)

If a node A is smaller than its child nodes (one of them), then A is not worthy of being the parent node, it should go down, and the larger node below will be the parent node, which is sinking A.

PriorityQueue

Priority queues no longer follow the first-in, first-out principle:

  • Maximum priority queue, regardless of the order of entry, the current largest element is dequeued first.
  • Minimum priority queue, regardless of the order of entry, the current smallest element is dequeued first.

In order to achieve this, the newly added elements need to be sorted so that the largest or smallest element is at the top of the queue. So it can be designed using a binary heap.

The priority queue has two main APIs, insert to insert an element and delMax to delete the largest element (if the bottom layer uses a min heap, then it is delMin)

insert enqueue

On the binary heap when enqueuingAdd a new element to the bottom of the heap, and then go up and down to maintain the structure of the binary heap.
Please add image description

delMin out

When dequeuing, delete the top element of the binary heap, then putReplace the bottom element of the stack to the top, and then go up and down to maintain the structure of the binary heap.
Please add image description

There is a priority queue in the STL library of C++, and Java also provides this data structure.
C# also added a priority queue in .Net 6. Some big guys have already parsed the source code, and it takes about 10 minutes to read it.
Source code analysis C# implementation of PriorityQueue (priority queue)

reference

Comic: What is a priority queue?
Source code analysis of the implementation of PriorityQueue (priority queue) in C#
Detailed explanation of binary heap and implementation of priority queue

Guess you like

Origin blog.csdn.net/weixin_44394801/article/details/122657684