"Data Structure" C + + code heap (priority queue)

Source: Internet
Author: User

Heap, which is one of the most common implementations of priority queues. In the priority queue, each element is given a priority, and the highest priority element is made out of the team each time it is out. Heap is a method of storing a priority queue, especially a priority queue stored in a tree form. The most commonly used is the two fork heap, but since it is specifically introduced data structure, it may be said that all, we take 4 typical heap for comparison, see the following table (this table and the notes below, from the first middle school in Guangdong province Zhongshan, "the characteristics and application of left-leaning tree", and made the words to amend and add content):

Project

Two-fork Pile

Left-leaning tree

Two-item heap

Fibonacci Heap

Build

O (N)

O (N)

O (N)

O (N)

Insert

O (LOGN)

O (LOGN)

O (LOGN)

O (1)

Take the minimum point

O (1)

O (1)

O (LOGN)

O (1)

Delete Minimum point

O (LOGN)

O (LOGN)

O (LOGN)

O (LOGN)

Delete any point

O (LOGN)

O (LOGN)

O (LOGN)

O (LOGN)

Merge

O (N)

O (LOGN)

O (LOGN)

O (1)

Space requirements

Minimum

Smaller

So so

Larger

Complexity of programming

Minimum

Lower

Higher

Very high

Here are a few notes:

0, two is called Bernoulli Heap, the left side of the tree is also called Zoo; the oblique heap is not listed in the table because its complexity is the same as the left-biased tree, and there is no need to list it separately. The code is simpler, but the constants are large and may degenerate into O (n) under special data. The relationship between it and the left-leaning tree, like the relationship between Splay and AVL, has no merit or disadvantage, the former is more concise and flexible while the latter is faster. In addition, I did not talk about the D-Heap, which is because this type of heap is only worth applying in contact with external memory, and I do not want to mention it. We do not use the exam, usually also do not use, so in my opinion, temporarily understand, understand, and then really use to go deep into the later.

1, two-item heap, the complexity of its "minimum point" is O (log n), can be improved to O (1). The method is to record the minimum node at any time and update the record only when an operation such as INSERT, delete, and merge is performed.

2, two heap, the average time complexity of its "insert" Operation is O (1), the worst time complexity is given in the table.

3, Fibonacci heap, its "delete minimum point" and "Delete any point" the time complexity of two operations are split time complexity.

Perhaps you do not understand the whole, then please self-search it, I just give the implementation code here. Because of the plethora of types, I only give one of the most common implementations of each heap (for example, a binary heap is just for an array of implementations), and you can digest the problem as needed. In addition, because the code is long, in order to facilitate the comparison, I just give the class version, the actual implementation, do not write classes sometimes rather convenient many.

Also, I still assume that the information for each element has only one int weight, which allows you to focus more on the framework of your code than in the cumbersome code. When real use, if the element information increases, depending on the situation can be modified.

Code:

//This code implements the minimum heapConst intMAXK =Ten;Const intMAXN =1024x768, inline swap (int&x,int&y) {intT=x; X=y; y=t;}//Two fork Pile//implements Init, in, out operationsclasstwo_heap{inth[maxn+1],l;//H[1~l] is the element in the heap    voidUpinti) {intJ=i>>1; if(i==1)return; if(H[i]>=h[j])return; Swap (h[i],h[j]);    Up (j); }    voidDowninti) {intj=i<<1; if(j>l)return; if(J!=l && h[j+1]&LT;H[J]) + +K; if(H[i]<=h[j])return; Swap (h[i],h[j]);    Down (j); } Public: Two_heap (): L (0) {}    voidInitintA[],intN) {//initializes the heap with an array of a[1~n], which is the process of building the heap in the heap, the Complexity O (N)L=N;  for(intI=1; i<=n;++i) h[i]=A[i];  for(intI=n>>1; i>=1;---) down (i);//It can be proved that from the N/2 start adjustment can be, after the N/2 point is necessarily a leaf node    }    void inch(intx) {a[++l]=x; up (L);} int  out() {Swap (a[1],a[l--]); Down1);returna[l+1]; }    intMin () {returna[1]; }};//not to be continued

"Data Structure" C + + code heap (priority queue)

Related Article

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.