1. What is a priority queue?
Let's take a look: when we go to KTV to sing songs, we can find that the songs we click are a queue.
At this time, a mm suddenly didn't play her cell phone and wanted to sing, so she came to Song and wanted her to turn as early as possible.
Therefore, she was able to select the "plug-in" function and insert it into the front queue.
Such a queue that can insert priority elements is called priority queue. However, this definition is not rigorous.
This is the basic model of the priority queue --
Two functions are available:
- Insert;
- Emin is the smallest deletion person.
Its job is --
It is very practical. Details can be used in operating systems, external sorting, and greedy algorithms.
2. How to implement priority queue?
1. Use a linked list
- Solution A: The chain table is unordered and inserted at the cost of O (1). The chain table is traversed at the cost of O (n) to delete the smallest element.
- Solution B: Keep the linked list in order and delete it at O (1). However, the insertion cost is relatively high, which is O (n );
Bigoal --
Generally, when the smallest delete operation is never redundant, solution a is better than solution B.
2. Search for a tree using binary forks
The binary search tree performs O (logn) on both operations ). It may be a bit too much, because the binary search tree can do far more functions than this, it is a bit suspected of killing chicken with a knife. Therefore, we will use a binary heap, a data structure that is more suitable than the half.
Iii. Binary heap
1. What is a binary heap?
The architecture and heap sequence, for example, is a binary heap.
Structure nature:A heap is a fully filled binary tree. The possible exception is that the underlying layer is filled with elements from left to right. This type of tree is called a Complete Binary Tree.