Priority queue (HEAP)

Source: Internet
Author: User

I. Simple implementation of priority queue:

1. Use a simple linked list to insert an O (1) in the header, and traverse the list to delete the smallest element. This requires O (n) time.

2. Keep the table in order. This results in a high insertion cost (O (n), while a low deletion cost (O (1 )). The fact that the operation based on the smallest element is never more than the insert operation is that the former is a better idea.

3. Use the binary search tree. The average running time of these two operations is O (logn ). Although insertion is random and deletion is not, this conclusion is true. The unique element to be deleted is the smallest element. Removing nodes from the left subtree repeatedly seems to damage the tree's balance and aggravate the right subtree. However, the right subtree is random. In the worst case, when the left subtree is deleted, the right subtree has the most elements. (There are many unnecessary operations in the Search Tree)

4. Tool binary heap (HEAP) to be used: its use is so common for the implementation of priority queue.

2. Heap has two properties: Structure and heap sequence.

1) structural nature: the heap is a fully filled binary tree. The possible exception is that it is at the bottom layer. The elements at the bottom layer are filled from left to right, that is, a completely binary tree.

The number of nodes in A Complete Binary Tree with a height of H is 2 ^ h ~ 2 ^ (H + 1)-1. This means that the Complete Binary Tree is O (logn ).

Because a completely binary tree is regular, it can be represented by an array without a pointer. For any position I in the array, the left child is at the 2I position, and the right child is in the next unit (2I + 1) of the left child, its parent node is on I/2 (rounded down.The only problem with this implementation is that the maximum heap size needs to be estimated in advance, but this is not a problem in typical cases.

Therefore, the data structure of a heap is composed of an array (regardless of the keyword type), an integer representing the maximum value, and the current heap size.

# Define minqusize 5
# Define mindata 0.0001
Typedef int elemtype;
Typedef struct heapstruct
{
Elemtype * ELEM;
Int capasity;
Int size;
} Heap, * priorityqueue;

2) Heap sequence nature: the nature of the operation to be quickly executed is heap sequence. to quickly find the minimum element, the minimum element should be on the root. Considering that any subtree should also be a heap, the benevolent node should be smaller than all its descendants.

In a heap, for each node X, except for the root node (which has no father), the keywords in the father of X are smaller than (or equal to) X ).

III. Basic operations:

1) insert: to insert an element x into the heap, we create a hole in the next idle position (at the end of the array). Otherwise, the heap is not a full tree. If X can be placed in a hole without damaging the heap sequence, the insertion is complete. Otherwise, place the elements on the parent node of the hole into the hole, so that the hole position moves up to the same direction as the following. Continue this process until X can be put into a hole. This operation is called filtering.

Void insertheap (elemtype value, priorityqueue H)
{
Int I;
If (H-> size = H-> capasity) // The heap is full.
Return;
For (I = ++ H-> size; H-> ELEM [I/2]> value; I/= 2)
{
H-> ELEM [I] = H-> ELEM [I/2]; // move the parent node down
}
H-> ELEM [I] = value;
}

2) deletemin: Delete the smallest element. When the smallest element is deleted, a hole is generated at the root node. Because one element is missing from the heap, the last element x in the heap must be moved to a location in the heap. If X can be put into a hole, the delete operation is complete. This is generally not possible. Therefore, the two sons of the holes are moved into the holes, so that the holes are pushed down to a layer. Repeat this step until X can be put into a hole. Therefore, we place X in a correct position starting from the root that contains the minimum son.

Elemtype deletemin (priorityqueue H)
{
Int I, child;
Elemtype minelem, lastelem;
If (H-> size = 0)
{
Cout <"priorityqueue is empty." <Endl;
Return H-> ELEM [0];
}

 

// Heap creation # include <iostream> # define minqusize 5 # define mindata 0.0001 using namespace STD; typedef int elemtype; typedef struct heapstruct {elemtype * ELEM; int capasity; int size;} heap, * priorityqueue; priorityqueue initializeheap (INT maxsize) {priorityqueue h; If (maxsize <minqusize) Throw ("priority queue size is too small. "); H = new heapstruct (); H-> ELEM = new int [maxsize + 1]; H-> capasity = maxsize; H -> Size = 0; H-> ELEM [0] = mindata; return h;} void insertheap (elemtype value, priorityqueue h) {int I; if (H-> size = H-> capasity) // The heap is full. Return; for (I = ++ H-> size; h-> ELEM [I/2]> value; I/= 2) {H-> ELEM [I] = H-> ELEM [I/2]; // move down the parent node} H-> ELEM [I] = value;} elemtype deletemin (priorityqueue h) {int I, child; elemtype minelem, lastelem; if (H-> size = 0) {cout <"priorityqueue is empty. "<Endl; retur N h-> ELEM [0];} minelem = H-> ELEM [1]; lastelem = H-> ELEM [H-> size --]; for (I = 1; I * 2 <= H-> size; I = Child) {Child = I * 2; // If (child! = H-> size & H-> ELEM [child]> H-> ELEM [Child + 1]) Child ++; if (lastelem> H-> ELEM [child]) H-> ELEM [I] = H-> ELEM [child]; // else break is moved down by holes ;} h-> ELEM [I] = lastelem; return minelem;} int main () {priorityqueue h; H = initializeheap (10); For (INT I = 1; I <8; I ++) insertheap (I, H); For (Int J = 0; j <= H-> size; j ++) cout 

Priority queue (HEAP)

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.