Minimum/large heap operations and heap sequencing

Source: Internet
Author: User

Excerpt from: "Aha algorithm"

We're going to use 1, 2, 5, 12, 7, 17, 25, 19, 36, 99, 22, 28, 46, 92来 to build the smallest heap, and remove the smallest number, and add a number 23

How to build this heap:

// Build a heap 0 ;  for (int1; I <= m; i++) {    n++    ; = A[n];    Shiftup (n);}

We also have a faster way to build a heap

Train of thought: Directly put 1, 2, 5, 12, 7, 17, 25, 19, 36, 99, 22, 28, 46, 92 into a complete binary tree, in this binary tree, we start from the last node, in turn, determine whether the subtree with this node as the root of the minimum heap characteristics. If all subtrees conform to the minimum heap characteristics, then the whole tree is the smallest heap. (Note that a full binary tree has a nature, and the last non-leaf node is the first N/2 node, starting from 1 and not from 0)

Algorithm: The n elements to build a heap, first we can the N nodes from the top-down, left-to-right way from 1 to n encoding, you can convert the N nodes into a complete binary tree. Then start from the last non-leaf node (node number N/2) to the root node (node number 1), scan all nodes one by one, and adjust the current node downward as necessary until the subtree of the root node is consistent with the properties of the heap. Time complexity is O (N)

The code is as follows:

void create_heap () {    for (int i = n/21; i--) {        shiftdown (i);}    } 

After the heap is completely binary tree, and all parent nodes are smaller than child nodes

Next, we will remove the heap top and put the newly added 23 on top of the heap.

It is obvious that the added number does not fit the minimum heap, and we need to adjust the number of new additions to the appropriate position.

The downward adjustment, which compares this number with its two sons 2 and 5, chooses the smaller one to swap with it

At this point we find that the minimum heap is still not satisfied, so we continue to exchange 23 with the smaller of its two sons.

Re-exchange

Downward-Tuned code:

voidShiftdown (inti) {//Pass in a node number I that needs to be adjusted downwards    intT, flag =0;//flag is used to mark the need to continue downward adjustment     whileI2<= n && Flag = =0) {        //first judge the relationship between it and the left son and use a node number with a lower value for the T record        if(H[i] > h[i*2]) {T= i*2; } Else{T=i; }        //if he has a right son, discuss the right son again.        if(i*2+1<=N) {//If the value of its right son is small, update the smaller node number            if(H[t] < h[i*2+1]) T= i *2+1; }        //If you find that the smallest number is not your own, the child nodes are smaller than the parent .        if(t! =i) {swap (t, i); I=T; } Else{flag=1; }    }}

If you just want to add a number instead of deleting the minimum, simply insert the new element at the end, and then determine whether the new element needs to move up, as appropriate, until the new feature position is met.

Join us now to join a 3

Comparing 3 to its parent node 25, the discovery is smaller than the parent node and needs to be exchanged with the parent node. And so on

Code to adjust upward:

//Add a new elementvoidShiftup (inti) {//Pass in a node number I that needs to be adjusted upward    intFlag =0;//use to mark if you need to continue to adjust upward    if(i = =1)return;//if it is a heap top, return it, no need to adjust the//is not at the top of the heap, and the value of the current node i is smaller than the parent node, continue to adjust upward     while(I! =1&& flag = =0) {        //determines whether the parent node is smaller than the        if(H[i] < h[i/2]) {Swap (I, I/2); } Else{flag=1; } I= i/2; }}

--------------------------------------------------------------------------------------------------

Heap Sort:

Time complexity is O (NLOGN), if you want to order from small to large, then only need to establish the minimum heap, and then each time the top element is deleted and the top element output or into a new array, until the heap is empty.

int Deletemax () {    int  t;     = h[1];    h[1] = H[n];    n// heap elements reduced by 1    Shiftdown (1// down adjustment     return  t;}

A better way is, from small to large sort of time not to build the smallest heap, but to build the largest heap, the largest heap after the establishment of the largest element in h[1], because our needs are small to large order, hope the largest in the end, so we will h[1] and H[n] Exchange, at this time H[n] is the largest element in the array. Adjust the h[1] downward to preserve the properties of the heap after swapping.

void Heapsort () {    while1) {        swap (1, n);        N--;        Shiftdown (1);}    }

-------------------------------------------------------------------------------------------------

Other applications of the heap:

1. Ask for the number of K in a series

A minimum heap of size k is established, and the top of the heap is the number of k large

For example, suppose there are 10 numbers that require the 3rd largest number, the first step is to select any 3 numbers, for example, the first 3, the 3 numbers are built into the smallest heap, and then the 4th number, and the top of the heap
Number comparison, if the number is smaller than the heap top, then this number is not, if the number is larger than the heap top, then discard the current heap top and the number as a new heap top, and then to maintain the heap

Minimum/large heap operations and heap sequencing

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.