Data Structure-Implementation of exercise 12 heap and optimal queue

Source: Internet
Author: User

Heap sorting is one of the most important sorting algorithms, which are often used in normal development and interviews. Stack features:

1. There is a Complete Binary Tree, 1;

2. It can be divided into the maximum heap and the minimum heap. The largest heap means that the data of any root node is no less than that of the left and right child nodes. The smallest heap means that the data of any node is no greater than that of the left and right child nodes;

3. The complexity of the heap sorting algorithm is O (nlgn), which is faster than bubble and insert. The reason is that the heap only maintains the local maximum or minimum.

4. array-based storage is used for Heap Storage;

Stored in the array:

Figure 1

The following describes the heap in four parts: heap insertion, deletion, and heap sorting, and the heap priority queue.

1. Heap insertion

Heap inserts can only be inserted at the last position, 2. It can only be inserted in a black circle. After insertion, update is required to maintain the heap nature, that is, the root data is the maximum or minimum. Update idea: From the insert node to the root node, compare and update until the conditions are met (this column is: the root data is smaller than the left and right children ).

General understanding:

The key code is as follows:

Note: The order of the left and right children is 2I + 1 and 2I + 2, respectively. Based on the preceding flowchart, the insert process is as follows: Compare from bottom to top, and re-adjust the heap structure.

Void minheapfixup (int * a, int I) {Int J, temp; temp = A [I]; j = (I-1)/2; // parent node while (j> = 0 & I! = 0) {if (a [J] <= temp) break; A [I] = A [J]; // move a large subnode down, replace its subnode I = J; j = (I-1)/2;} A [I] = temp;} void minheapaddnumber (int * a, int N, int nnum) {A [n] = nnum; minheapfixup (A, n );}

Ii. Delete heap

Remember to delete only the elements at the top, put the elements at the lowest end to the top, and update the entire heap from top to bottom. As follows:

Void minheapfixdown (int A [], int I, int N) {Int J, temp; temp = A [I]; j = 2 * I + 1; while (j <n) {If (J + 1 <n & A [J + 1] <A [J]) // if a child exists, compare the size of left and right children J ++; if (a [J]> = temp) break; A [I] = A [J]; I = J; j = 2 * I + 1;} A [I] = temp;} void minheapdeletenumber (int A [], int N) {STD: swap (A [0], A [n-1]); minheapfixdown (A, 0, n-1 );}

Conclusion: delete: only the header node can be deleted, and the heap can be adjusted from top to bottom. insert, only at the end, and then adjust the heap from bottom to top. 3. Heap array. Use the insert idea to adjust the heap from bottom to top. Adjust each non-leaf node. The key code is as follows:

void MakeMinHeap(int* a, int n)  {      for (int i = n / 2 - 1; i >= 0; i--)      MinHeapFixdown(a, i, n);  }  
Test: combined with the data in the image, we will demonstrate it again:
Int main () {int A [8] = {9, 7, 5, 6, 8, 4, 10}; cout <"the data in the original array is :"; for (INT I = 0; I <7; ++ I) cout <A [I] <""; cout <Endl <"the heap array is:"; makeminheap (A, 7); For (INT I = 0; I <7; ++ I) cout <A [I] <"; cout <Endl <" adds an array after data 3: "; minheapaddnumber (A, 7, 3 ); for (INT I = 0; I <8; ++ I) cout <A [I] <""; minheapdeletenumber (A, 8 ); cout <Endl <"data after the vertex is deleted:"; for (INT I = 0; I <7; ++ I) cout <A [I] <"; return 0 ;}

We naturally need to ask: What is the significance of maintaining a heap structure? There are at least two purposes: heap sorting and priority queue. 4. Heap sorting. Thought: since we can extract the vertex data every time and the obtained data is the smallest data, according to this idea, we can obtain all the data in the array in order. However, like merging and sorting, the space complexity increases and an array must be accepted. Test:

int b[8];    for(int i=0;i<8;++i){b[i]=a[0];    MinHeapDeleteNumber(a,8-i);}for(int i=0;i<8;++i)cout<<b[i]<<" ";

We know that any algorithm has to calculate the time complexity. Otherwise, the algorithm is meaningless. Because of tree-based heap and tree height lgn, in the worst case, each layer is calculated n times, so the complexity is: O (nlgn), which is the same as merging. Optimal queue: the optimal queue is the maximum or minimum value of data popped up each time. An update operation is required each time a queue is entered. A delete operation is required for each departure.

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.