Heap-The magical priority queue

Source: Internet
Author: User
Tags constant data structures sort

Introduction (from http://blog.csdn.net/changyuanchn/article/details/14564403)

In the front already has the list, the stack, the queue, the tree and so on the data structure, especially the tree, is a very powerful data structure, can do many things, then why also introduce a priority queue of Dongdong. What is the nature of the difference between it and the queue? Look at an example, there is a printer, but there are a lot of files to print, then these tasks must be queued to wait for the printer to process gradually. This creates a problem. In principle, first of all, but there is a file 100 page, which is ranked in the front of another 1 pages of the file, then it may be necessary for us to print this 1 page file is more reasonable. Therefore, in order to solve this kind of problem, the priority queue model is proposed.

The priority queue is a data structure that provides at least insert (insert) and delete minimum (deletemin) operations. The operation corresponding to the queue, insert equivalent to enqueue,deletemin equivalent to dequeue.

Linked lists, binary lookup trees, can provide both insertion (insert) and delete minimum (deletemin) operations, but why not use them to introduce new data structures. The reason is that the pre-application requires a high degree of time complexity. For the implementation of the list, insert requires O (1), delete minimum need to traverse the list, it is required O (N). For binary lookup trees, both operations require O (LOGN), and with the constant deletemin operation, the binary lookup tree becomes very unbalanced, while using a two-fork lookup tree is wasteful, so many operations are not needed at all.

So here is a new data structure that enables the worst-case complexity of both insert and delete-minimum (deletemin) operations to be O (N), while the average time complexity of insertions is constant time, i.e. O (1). You do not need to introduce pointers at the same time.

When we delete the smallest number in an array and then insert a number, when the operation is more frequent, or the data is too large, this sort of operation, or the insertion of the operation, time is more complex, so we use the heap for data processing.

The heap is also used to store the array, it is a special complete binary tree, according to their nature is divided into the minimum heap and the largest heap, the minimum heap refers to each father node is smaller than the child node, the largest heap on the other hand, because of this characteristic, so for us to sort, after the insertion of the sorting structure is convenient.

Deleted post-insertion adjustment




Do not delete the inserted adjustment


<span style= "FONT-SIZE:14PX;" >void siftleft (int i)
{
    int flag=0;
    if (i==1)
        return;
    while (i!=1 && flag==0)
    {
        if (H[I]<H[I/2])
        {
            swap (I,I/2);
        }
        else
            flag=1;
        I=I/2;
    }
} </span>

Full output code from small to large

 #include <iostream> using namespace std; int n,h[1000]; void swap (int a,int b) {int c;
    C=h[a];
    H[A]=H[B];
H[b]=c;
    } void Siftdown (int i) {int flag=0,t;
        while (I*2<=n && flag==0) {if (h[i]>h[i*2]) {t=i*2;
        } else t=i;
            if (i*2+1<=n) {if (h[t]>h[i*2+1]) {t=i*2+1;
            }} if (T!=i) {swap (t,i);
        i=t;
        } else {flag=1;
    }}} void Create () {for (int i=n/2;i>=1;i--) {siftdown (i);
    }} int Delete_ () {int t;
    T=H[1];
    H[1]=h[n];
    n--;
    Siftdown (1);
return t;
    } int main () {int i,num;
    cin>>num;
    for (i=1;i<=num;i++) cin>>h[i];
    N=num;
    Create ();
    for (i=1;i<=num;i++) {printf ("%d", Delete_ ()); }
}



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.