-
- Priority queue
- Heap
- 1 Heap-based algorithms
- Initialization
- Heap up from bottom up
- Top-down stacking
- Insert Delete one item
- 2 Heap Sorting
- Priority queue All code
1 priority queue
A normal queue is a first-in-one data structure, where elements are appended at the end of the queue and removed from the queue header. In the priority queue, the element is given a priority. When an element is accessed, the element with the highest priority is deleted first. The priority queue has the behavior characteristics of highest first out (Largest-in,first-out).
The priority queue supports two basic operations : Inserting a new data item into the priority queue and deleting the largest item of the keyword in the (maximum) priority queue. The priority queue is flexible enough to support operations:
- constructs a priority queue based on n data items
- Insert a data item
- Delete data item with maximum value
- Change the priority of any given data item
- Delete any given data item
- Merge two priority queues into one priority queue
Sort: Insert all records first, then delete the largest elements one by one to get the records in reverse order.
[note] Studying various data structures, we will remember two basic tradeoff strategies: linked list memory allocation and sequential memory allocation (arrays).
Different implementations have different performance characteristics for the various operations to be performed, and different application problems require efficient performance of different sets of operations. such as: Ordered table Delete fast insert slow, unordered table Delete slow insert fast.
2 stacks
The data structure of the heap, which can support the basic operation of the priority queue. In the heap, the keywords in the parent node are greater than or equal to the keywords in their child nodes.
If the keyword for each node in a tree is greater than or equal to the keyword (if any) of all child nodes, the tree is called a heap-ordered.
A heap is a collection of nodes, represented as an array, in which the keywords are arranged in the form of a complete binary tree in a heap order. intuitively, we should use a linked list to represent a heap of ordered trees, but a fully binary tree gives us the opportunity to use a compressed array representation.
At the parent node at position I, the positions of the two child nodes are 2i and 2i+1, respectively.
2.1 Heap-based algorithms
If we make a simple modification to the heap-based priority queue, we can violate the conditions of the heap, and then, by traversing the heap and modifying the heap to meet the conditions of the heap when needed, we call this process a heap or remediation heap.
There are two situations in which a remediation heap is required:
(1) The priority of a node is increased or a new node is inserted into the bottom of the heap and needs to be traversed up the heap repair
(2) The priority of a node is lowered or the maximum item is removed, and the heap repair needs to be traversed down
Initialization
//优先队列数据结构struct PQ{ Item data[maxN]; int N;};//初始化PQ* PQInit(){ PQ* pq = (PQ*)malloc(sizeof(*pq)); 0; return pq;}
Heap up from bottom up
//Insert一项时,从数组尾部插入,向上修复voidPQFixUp(PQ *pq){ int k = pq->N; // k节点的父节点是 k/2 1 && less(pq->data[k/2], pq->data[k]))//共比较 lg N 次 { exch(pq->data[k/2], pq->data[k]); k = k/2; } }
Top-down stacking
If the node keyword is smaller than the keyword of one or two child nodes, then swapping the node with a larger child node will affect the child node not satisfying the heap nature, so fix it in the same way.
Remove the maximum (top) node from top to bottom fixvoid Pqfixdown(PQ*PQ) {int k =1; IntN= pq->N; While2*k <=N)//Total comparison2*lgNTimes (Find larger child nodes LGN, decide whether to swap LGN) {int J =2*k;if(J <N&& Less (pq-> Data[j], pq->data[j+1]) Find larger child nodesj + +;if(!less (pq-> Data[K], pq->data[j]) Fix complete when the parent node is not smaller than the maximum child nodeBreak Exch (pq-> Data[K], pq->data[j]);K = J; }}
Insert, delete an item
//InsertAn element, insert to the end of the array, and then fix it upvoid Pqinsert(PQ*PQ,ItemV) {pq-> Data[+ +(PQ-N)] = V; Pqfixup(PQ);} Delete max-the first node; fix downItem Pqdelmax(PQ*PQ) {if(Pqisempty(PQ)) {printf ("Error:pq is empty. Can ' t delete max\n "); Return-1; } Exch (pq-> Data[1], pq->data[pq->N]);//Exchange the largest and last item(pq->N)--;//culling last item (max) Pqfixdown(PQ); Return pq-> data[pq->N + 1];}
If the unordered table pair is sorted with a selection when deleted, the ordered table corresponds to the Insert sort
2.2 Heap Sorting
voidintint r){ PQ* pq = PQInit(); int i; for(i = l; i <= r; i++) PQInsert(pq, a[i]); for(i = r; i >= l; i--) a[i] = PQDelMax(pq);}
3 Priority Queue All code
/*======================================================title: Array-based priority queue implementation: K parent node, 2k, 2k+1 the child node data items are stored in the 1~n item of the array. Functions: Inserts an item, keeps the heap up in order, deletes and returns the largest item, and keeps the order down by stacking author:quinndate:2015/03/27======================================= ================*/#include <stdlib.h>#include <stdio.h> maximum capacity of #define MAXN//Queue typedef intItem;typedef structPQ PQ;#define EXCH (A, B) {Item t; t = A; A = B; B = t;}#define LESS (A, B) (a < b)//Priority queue data structurestructpq{Item DATA[MAXN];intN;};//Initializepq* Pqinit () {pq* PQ = (pq*)malloc(sizeof(*PQ)); Pq->n =0;returnPQ;}BOOLPqisempty (PQ *pq) {returnPq->n = =0;}//insert an item, insert from the end of the array, fix upvoidPqfixup (PQ *pq) {intK = pq->n; while(k >1&& Less (pq->data[k/2], Pq->data[k]))//Compare LG N times{Exch (pq->data[k/2], pq->data[k]); K = k/2; } }//delete max (top) node, from top to bottom fixvoidPqfixdown (PQ *pq) {intK =1;intN = pq->n; while(2*k <= N)//Compare 2*lg N times (Find larger sub-nodes LGN, determine if Exchange LGN){intj =2*k;if(J < N && Less (pq->data[j], pq->data[j+1]) J + +;if(!less (Pq->data[k], pq->data[j]))fix complete when//parent node is not smaller than the maximum child node Break; Exch (Pq->data[k], pq->data[j]); K = J; }}//insert an element, insert the tail of the array, and then fixvoidPqinsert (PQ *pq, Item v) {pq->data[++ (pq->n)] = v; Pqfixup (PQ);}//delete max-first node; fix downItem Pqdelmax (PQ *pq) {if(Pqisempty (PQ)) {printf("Error:pq is empty. Can ' t delete max\n ");return-1; } Exch (pq->data[1], pq->data[pq->n]); (pq->n)--; Pqfixdown (PQ);returnPq->data[pq->n +1];}intMain () {pq* PQ = Pqinit (); for(inti =0; I <Ten; i++) Pqinsert (PQ, i); for(inti =0; I < One; ++i) {printf("%d\n", Pqdelmax (PQ)); }}
Priority queue and heap ordering