[Data structure] priority queue and heap

Source: Internet
Author: User

Priority queue)

Generally, a queue adds data at the end of the queue and deletes the data in the header to enable FIFO.

Each element in the priority queue has a priority. Each time an element is put into the queue, the element with the highest priority is sent out of the queue, which is called largest-in, first-out ).

The priority queue must perform the following operations:

1. Push: put an element into the queue.

2. Pop: removes the element with the highest priority from the queue.

To achieve the above operation, you can maintain an ordered linked list. Each time data is inserted, the entire linked list is maintained in order, that is, the insertion method is used. Each time you leave the queue, you only need to delete the first element of the linked list.

In this case, the complexity of the team-entry operation is O (n), the team-out is O (1), and the overall complexity is O (n ).

Next we will introduce the data structure of O (logn)-binary heap.

Given a Complete Binary Tree, each node of the tree has a higher priority than all child nodes of the node. It is easy to know that the root node will be the node with the highest priority.

To use a binary heap to implement a priority queue, you must complete two operations:

1. Push: For an empty binary heap, you only need to add the elements in the heap to the root node. In general, we add this element directly to a position where the Complete Binary Tree is located. This position refers to the point where the position is added to the complete binary tree, which is still the Complete Binary Tree. Then compare the element with its parent node. If the priority is higher than the parent node, the element is switched to the parent node, and then the newly inserted element is compared with its new parent node, it does not have a parent node or has a parent node with a higher priority.

2. Pop: to obtain the root node of the Complete Binary Tree, maintain the remaining node to satisfy the nature of the binary heap.

Before talking about this operation, we should first define a new operation. Assume that both the left and right trees of a node meet the nature of the heap, and the entire tree should also be changed to a heap. We call this operation sift ). For filtering algorithms, we need to compare the root node with the two subnodes. If the root node has the highest priority, it has been adjusted, otherwise, the larger child nodes will be exchanged with the root node. Then the root node will be the largest element, and the location of the child node to be switched will be tracked, continue to make the above adjustments until the adjustments are completed.

For a team-out operation, delete the root node element and delete the last element of the binary heap, the binary tree is still the element of the Complete Binary Tree) is placed at the root node, and then the tree is filtered.

 

In general, the heap can be easily implemented using arrays. If the element numbered 1 in the array is the root node, then 2 and 3 are the left and right nodes of the array. For a node whose array number is I (I> = 1), its left and right nodes are I * 2 and I * 2 + 1 respectively.

To directly change n elements (numbers from 1 to n) in an array to a heap, perform the following operations: for (I = n/2; I> = 1; I --) sift (the node numbered with I is the root node ). This is because those nodes that exceed n/2 are leaf nodes that satisfy the heap nature. Other nodes perform the sift operation from the bottom up to create the heap.

1 # include <stdio. h> 2/* R [0]. fix is the heap length */3 typedef int elemtype; 4 typedef struct node 5 {6 elemtype data; 7 int fix; 8} heapnode; 9 heapnode out (heapnode R [], int S)/* R [s] heap */10 {11 int I, j; 12 heapnode item; 13 item = R [s]; /* temporary data storage */14 R [s] = R [R [0]. fix];/* place the last data in the position of the element to be heap */15 R [0]. fix --;/* length minus 1 */16 sift (R, S, R [0]); 17 return item; 18} 19 void in (heapnode R [], heapnode item)/* heap */20 {21 int I; 22 for (I = R [0]. fix + 1; I> 1; I/= 2) 23 {24 if (item. fix </*> */R [I/2]. fix) R [I] = R [I/2]; 25 else break; 26} 27 R [I] = item; 28 R [0]. fix ++; 29} 30 void sift (heapnode R [], int S, int T) /* filter to small top heap algorithm */31 {/* R [s... all records in T] Except R [s] Meet the heap characteristics */32 int I, j; 33 heapnode item; 34 Item = R [s]; 35 I = s; 36 J = 2 * I; 37 while (j <= R [0]. fix) 38 {39 if (J + 1 <= R [0]. fix & R [J + 1]. fix </*> */R [J]. fix) J ++; 40 if (item. fix>/* <*/R [J]. fix) {R [I] = R [J]; I = J; j = 2 * I;} 41 else break; 42} 43 R [I] = item; 44} 45/* 46 initial heap creation: 47 for (I = n/2; I> = 1; --) sift (R, I, n ); 48 */49/* heap sorting */50/* the Big Top heap completes the ascending order, and the small top heap completes the descending order */51 void heat_sort (long R [], int N) 52 {53 int I; 54 long item; 55 for (I = n/2; I> = 1; I --) sift (R, I, n ); /* create an initial heap */56 for (I = N; I> = 2; I --) 57 {58 Item = R [I]; 59 R [I] = R [1]; 60 R [1] = item; 61 sift (R, 1, I-1);/* The Large top heap completes ascending order, the small top heap completes descending order */62} 63}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.