STL Source Code Analysis algorithm Stl_heap.h

Source: Internet
Author: User

This article Senlie the original. Reprint please keep this address:Http://blog.csdn.net/zhengsenlie


Heap

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

Binary heap is a completely binary tree.


Implicit notation: The tree is expressed as an array.
Tip: Keep the #0 elements of the array. Then the left and right child nodes of element I are 2i and 2i + 1 each,
The parent node is I/2---and the STL doesn't use such a small trick
Array cannot be dynamically resized, so vector is substituted for array
This file provides algorithms for various heap operations, noting that there are no heap classes
Figure 4-20


Demo Sample:

#include <vector> #include <iostream> #include <iterator> #include <algorithm>using namespace Std;void Display (const vector<int> &v) {copy (V.begin (), V.end (), ostream_iterator<int> (cout, "")); cout << Endl;} int main () {int ia[] = {0,1,2,3,4,8,9,3,5};vector<int> Ivec (ia, IA + sizeof (IA)/sizeof (int)); Make_heap (Ivec.begin (), Ivec.end ());d isplay (Ivec), Ivec.push_back (7);p Ush_heap (Ivec.begin (), Ivec.end ());d isplay (Ivec);p op_heap ( Ivec.begin (), Ivec.end ()), Ivec.pop_back ();d isplay (Ivec), Sort_heap (Ivec.begin (), Ivec.end ());d isplay (Ivec);

Source:
#ifndef __sgi_stl_internal_heap_h#define __sgi_stl_internal_heap_h__stl_begin_namespace#if defined (__SGI) & &!defined (__gnuc__) && (_mips_sim! = _mips_sim_abi32) #pragma set Woff 1209#endiftemplate <class                 Randomaccessiterator, Class Distance, Class T>void __push_heap (randomaccessiterator First, Distance Holeindex,  Distance TopIndex, T value) {Distance parent = (holeIndex-1)/2; Locate the parent node//when the top has not been reached. And the parent node is less than the new value (and therefore does not conform to the order attribute of the heap) while (Holeindex > TopIndex && * (first + parent) < value) {* (First + Holeind ex) = * (first + parent); The value of the hole is Holeindex = parent; Adjust the hole number. Ascend to parent node (HOLEINDEX-1)/2; The parent node of the New Hole} * (first + Holeindex) = value; The value of the hole is the new value. Finished inserting operation}template <class Randomaccessiterator, Class Distance, class T>inline void __push_heap_aux ( Randomaccessiterator First, Randomaccessiterator last, distance*, t*) {__push_heap (First, Dis       Tance ((Last-first)-1), Distance (0),        T (* (last-1)));} /* When calling this function, the new element should have been placed at the end of the bottom container--and when? --Stl_heap is not open properly, and is used only within the STL, other STL programs that use PUSH_HEAP should be aware that before invoking the Push_heap function, the new element is placed at the end of the heap at the bottom of the container */template <class R andomaccessiterator>inline void Push_heap (randomaccessiterator first, Randomaccessiterator last) {__push_heap_aux (First, last, Distance_type (first), Value_type (first));} Template <class Randomaccessiterator, class Distance, Class T, class Compare>void __push_heap ( Randomaccessiterator First, Distance Holeindex, Distance topindex, T value, Compare comp) {Distance pare  NT = (holeIndex-1)/2;    while (Holeindex > TopIndex && Comp (* (first + parent), value) {* (first + Holeindex) = * (first + parent);    Holeindex = parent;  Parent = (holeIndex-1)/2; } * (first + Holeindex) = value;} In the STL there are so many functions in the body of such a function that a function is called directly. It doesn't feel so good. Add the cost of the stack,//Why not directly call inside that function template <class Randomaccessiterator, class Compare, Class Distance, class T>inline void __push_heap_aux (RaNdomaccessiterator First, Randomaccessiterator last, Compare comp, D istance*, t*) {__push_heap (First, Distance ((Last-first)-1), Distance (0), T (* (last-1)), comp);} Template <class Randomaccessiterator, class compare>inline void Push_heap (Randomaccessiterator first, Randomaccessiterator last, Compare comp) {__push_heap_aux (First, last, comp, Distance_type (first), Value_type (first));} Template <class Randomaccessiterator, class Distance, Class T>void __adjust_heap (Randomaccessiterator First,  Distance Holeindex, Distance len, T value) {Distance TopIndex = Holeindex;  Distance secondchild = 2 * holeindex + 2;    while (Secondchild < Len) {if (* (first + Secondchild) < * (first + (secondChild-1))) secondchild--;    * (first + Holeindex) = * (first + secondchild);    Holeindex = Secondchild;  Secondchild = 2 * (Secondchild + 1); } if (Secondchild = = LEN) {* (first + Holeindex) = * (First + (secondChild-1));  Holeindex = secondChild-1; } __push_heap (First, Holeindex, TopIndex, value);} 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);}                           Template <class Randomaccessiterator, class t>inline void __pop_heap_aux (Randomaccessiterator first, Randomaccessiterator last, t*) {//pop The result of the operation should be the first element of the bottom container __pop_heap (primary, Last-1, last-1, T (* (last-1)) , Distance_type (first));} Template <class randomaccessiterator>inline void Pop_heap (randomaccessiterator first, Randomaccessiterator last ) {__pop_heap_aux (First, Last, Value_type (first));} Template <class Randomaccessiterator, class Distance, Class T, Class Compare>void __adjust_heap (RandomacCessiterator First, Distance Holeindex, Distance len, T value, Compare comp) {Distance TopIndex = hole  Index; Distance secondchild = 2 * holeindex + 2; The right child node of the hole node while (Secondchild < len) {//When there is also a left and right child node if (comp (* (first + secondchild), * (First + (secondChild-1))    )) secondchild--; * (first + Holeindex) = * (first + secondchild); Let the larger value be the hole value Holeindex = Secondchild; Move the hole down to the larger sub-node Secondchild = 2 * (Secondchild + 1);    Find the child node of the new Hole node} if (Secondchild = = len) {//When there are only left nodes * (first + Holeindex) = * (First + (secondChild-1));  Holeindex = secondChild-1; } __push_heap (First, Holeindex, TopIndex, value, comp);} Template <class Randomaccessiterator, class T, Class Compare, class Distance>inline void __pop_heap (  Randomaccessiterator First, Randomaccessiterator last, randomaccessiterator result, T value, Compare    Comp, distance*) {//the first value (i.e. the value to be popped) is stored at the point where result is indicated.  *result = *first; Another adjustment heaP. The hole number is 0. To adjust values to value __adjust_heap (first, Distance (0), Distance (Last-first), value, comp);} Template <class Randomaccessiterator, class T, class Compare>inline void __pop_heap_aux (randomaccessiterator First, Randomaccessiterator last, t*, Compare comp) {__pop_heap (First, last-1, Last-1, T (* (last-1)), comp, Distance_type (first));} Template <class Randomaccessiterator, class compare>inline void Pop_heap (Randomaccessiterator first, Randomaccessiterator last, Compare comp) {__pop_heap_aux (First, Last, Value_type (first), comp);} 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-2)/2;    while (true) {__adjust_heap (first, parent, Len, T (* (first + parent));    if (parent = = 0) return; parent--; }}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 Compare, Class T, class Distance>void __make_heap ( Randomaccessiterator First, Randomaccessiterator last, Compare comp, t*, distance*) {if (Last-first &L T  2) return;  Distance len = Last-first; Find the head of the first subtree that needs to be re-queued. marked with the parent.

Distance parent = (len-2)/2; while (true) {__adjust_heap (first, parent, Len, T (* (first + parent)), comp); if (parent = = 0) return; Go through the root node. It's over. parent--; It's actually a left-to-right adjustment along the first parent node so that the parent node has a value greater than (or less than) two child nodes}}template <class Randomaccessiterator, class compare>inline void make _heap (randomaccessiterator First, Randomaccessiterator last, Compare comp) {__make_heap (First, last , Comp, value_type (first), Distance_type (first));} Template <class randomaccessiterator>void sort_heap (randomaccessiterator First, Randomaccessiterator last) {// Each time the Pop_heap () is run, the extremum is placed at the end//minus the tail end and run once again pop_heap (). The second extremum is placed in the new end//has been so, finally can get the sort result while (Last-first > 1) pop_heap (first, last--);} Template <class Randomaccessiterator, class Compare>void Sort_heap (Randomaccessiterator First, Randomaccessiterator last, Compare comp) {while (Last-first > 1) pop_heap (first, last--, comp);} #if defined (__SGI) &&!defined (__gnuc__) && (_Mips_sim! = _mips_sim_abi32) #pragma reset woff 1209#endif__stl_end_namespace#endif/* __sgi_stl_internal_heap_h */// Local variables://mode:c++//End:



Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

STL Source Code Analysis algorithm Stl_heap.h

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.