Heap algorithm (heap algorithm) in STL)

Source: Internet
Author: User
Zookeeper

① Push_heap Algorithm
The following is the implementation details of the push_heap algorithm. This function receives two iterators to represent the head and tail of a container (vector) at the bottom of a heap, and the new element has been inserted to the tail end at the bottom.
Template <class randomaccessiterator>
Inline void push_heap (randomaccessiterator first, randomaccessiterator last)
{
// Note that when this function is called, the new element should have been placed at the tail end of the bottom container
_ Push_heap_aux (first, last, distance_type (first), value_type (first ));
}

Template <class randomaccessiterator, class distance, class T>
Inline void _ push_heap_aux (randomaccessiterator first, randomaccessiterator last,
Distance *, T *)
{
// The above lines are based on the heap structure characteristics: the new value must be placed at the tail end of the bottom container, that is, the first hole number: (last-first)-1
_ Push_heap (first, distance (last-first)-1), distance (0), T (* (last-1 )));
}

Template <class randomaccessiterator, class distance, class T>
Void _ push_heap (randomaccessiterator first, distance holeindex, distance topindex, T value)
{
Distance parent = (holeIndex-1)/2;
While (parent> topindex & * (first + parent) <value)
{
* (First + holeindex) = * (first + parent );
Holeindex = parent;
Parent = (holeIndex-1)/2;
}
* (First + holeindex) = value;
}
② Pop_heap Algorithm
After the pop operation removes the root node (in fact, it is set to the tail node of the vector at the bottom of the container), in order to meet the condition of the Complete Binary Tree, the leaf node at the bottom and rightmost must be removed, and re-install the value to the maximum heap.
Template <class randomaccessiterator>
Inline void pop_heap (randomaccessiterator first, randomaccessiterator last)
{
_ Pop_heap_aux (first, last, value_type (first ));
}

Template <class randomaccessiterator, class T>
Inline void _ pop_heap_aux (randomaccessiterator first, randomaccessiterator last, T *)
{
_ Pop_heap (first, last-1, last-1, t (* (last-1), distance_type (first ));
}

Template <class randomaccessiterator, class T, class distance>
Inline void _ pop_heap (randomaccessiterator first, randomaccessiterator last, randomaccessiterator result,
T value, distance *)
{
* Result = * first;
_ Adjust_heap (first, distance (0), distance (last-first), value );
// If you want to re-adjust the heap, the hole number is 0 (that is, at the root of the tree), and the value to be adjusted is value (the original end value)
}

Template <class randomaccessiterator, class distance, class T>
Void _ adjust_heap (randomaccessiterator first, distance holeindex, distance Len, T value)
{
Distance topindex = holeindex;
Distance secondchild = holeindex * 2 + 2;
While (secondchild <Len)
{
If (* (first + secondchild) <* (first + secondChild-1 ))
Secondchild --;
* (First + holeindex) = * (first + secondchild );
Holeindex = secondchild;
Secondchild = holeindex * 2 + 2;
}
If (secondchild = Len)
{
* (First + holeindex) = * (first + secondChild-1 );
Holeindex = secondChild-1;
}
_ Push_heap (first, holeindex, topindex, value );
}
Note: After pop_heap, the maximum element is placed at the tail end of the container at the bottom and has not been removed. To obtain its value, you can use the back () operation function provided by the bottom container (vector. To remove it, you can use the pop_back () operation function provided by the bottom container (vector.
③ Sort_heap Algorithm
Since each pop_heap operation can obtain the largest heap key value element, if you continue to perform the pop_heap operation on the entire heap, each time you scale down an element from the back to the front (because pop_heap places the element with the largest key value at the end of the container at the bottom), when the entire program is executed, we have an incremental sequence.
Template <class randomaccessiterator>
Void sort_heap (randomaccessiterator first, randomaccessiterator last)
{
While (last-first> 1)
Pop_heap (first, last --);
}
④ Make_heap Algorithm
This algorithm is used to convert a piece of existing data into a heap.
Template <class randomaccessiterator>
Inline void make_heap (randomaccessiterator first, randomaccessiterator last)
{
_ Make_heap (first, last, value_type (first), distance_type (first ));
}

Template <class randomaccessiterator, class T, class distance>
Void _ make_heap (randomaccessiterator first, randomaccessiterator last, T *, distance *)
{
If (last-first <2) return;
Distance Len = last-first;
Distance parent = (len-1)/2;

While (true)
{
_ Adjust_heap (first, parent, Len, T (* (first + parent )));
If (parent = 0)
Return;
Parent --;
}
}

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.