STL source code analysis --- stl_heap.h Reading Notes

Source: Internet
Author: User
Heap heap is a common data structure and can also store elements in heap. However, the STL does not provide a heap container, but provides an algorithm for heap operations. All containers that support randomaccessiterator can be used as heap containers.
Heap is divided into Max heap and Min heap. The heap structure has the largest central node in each node extracted from Max heap, And the heap structure has the smallest central node in each node extracted from Min heap.
In practical applications, vector is often used as the heap container, and heap is often used as the priority queue. The http://blog.csdn.net/kangroger/article/details/8718732 is described here for the binary heap
When an element is inserted into the heap, It is inserted to the end and "maintained up": it refers to comparing the inserted node with its parent node. If it does not meet the heap requirements, it is exchanged, maintain the parent node up ......
When an element is removed from the heap, place the end element in the heap header and "maintain downward": this means that the parent node is compared with the child node. If the child node does not meet the requirements, exchange with a large (small) node, and maintain the child node of the exchange ......

Heap nodes cannot be traversed, So heap does not have an iterator.

G ++ 2.91.57, Cygnus \ cygwin-b20 \ include \ G ++ \ stl_heap.h complete list/*** copyright (c) 1994 * Hewlett-Packard Company ** permission to use, copy, modify, distribute and modify this software * and its documentation for any purpose is hereby granted without handle, * provided that the above copyright notice appear in all copies and * that both that copyright notice and this permission notice appear * In supportin G documentation. hewlett-Packard Company makes no * representations about the suitability of this software for any * purpose. it is provided "as is" without express or implied warranty. ** copyright (c) 1997 * Silicon Graphics Computer Systems, Inc. ** permission to use, copy, modify, distribute and merge this software * and its documentation for any purpose is hereby granted without tables, * provi DED that the above copyright notice appear in all copies and * that both that copyright notice and this permission notice appear * in supporting documentation. silicon Graphics makes no * representations about the suitability of this software for any * purpose. it is provided "as is" without express or implied warranty. * // * Note: This is an internal header file, encoded ded by other STL headers. * y Ou shoshould not attempt to use it directly. */# ifndef _ sgi_stl_internal_heap_h # DEFINE _ DEFINE # If defined (_ SGI )&&! Defined (_ gnuc _) & (_ mips_sim! = _ Mips_sim_abi32) # pragma set woff 1209 # endif // maintains the template <class randomaccessiterator, class distance, class T> void _ push_heap (sequence first, distance holeindex, distance topindex, T value) {distance parent = (holeindex-1)/2; // identify the parent node // The parent node is not the heap top and the value of the parent node is smaller than the child node (smaller than the number <we can see that the STL maintains the max heap) while (holeindex> topindex & * (first + parent) <value) {* (first + holeindex) = * (first + P Arent); // holeindex = parent down for the parent node; // maintain the parent node parent = (holeindex-1)/2;} // after it reaches the top of the heap or meets the heap nature, find the appropriate position * (first + holeindex) = value;} template <class randomaccessiterator, class distance, class T> inline void _ push_heap_aux (randomaccessiterator first, last, distance *, T *) {_ push_heap (first, distance (last-first)-1), distance (0 ), T (* (last-1);} // Add an element to heap. Note: When this function is called, the element has been added to the end, and the iterator has added 1 template <class randomaccessiterator> inline void push_heap (randomaccessiterator first, randomaccessiterator last) {// note that when this function is called, the new elements must be placed at the end of the bottom-end container. _ Push_heap_aux (first, last, distance_type (first), value_type (first);} // maintains upwards. You can use your own defined comp function, this is not necessarily Max heap. template <class randomaccessiterator, class distance, class T, class compare> void _ push_heap (randomaccessiterator first, distance holeindex, distance topindex, T value, compare comp) {distance parent = (holeindex-1)/2; // locate the parent node position while (holeindex> topindex & Comp (* (first + parent), Val UE) {* (first + holeindex) = * (first + parent); holeindex = parent; parent = (holeindex-1)/2;} * (first + holeindex) = value;} template <Class Identifier, class compare, class distance, class T> inline void _ push_heap_aux (randomaccessiterator first, randomaccessiterator last, compare comp, distance *, 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);} // The following _ adjust_heap () the specified heaptemplate <class randomaccessiterator, class distance, class T> void _ adjust_heap (randomaccessiterator first, distance holein Dex, distance Len, T value) {distance topindex = holeindex; distance secondchild = 2 * holeindex + 2; // locate the right child position while (secondchild <Len) {// The child size is not reached yet. // compare the size of the left and right children. secondchild indicates the larger child, if (* (first + secondchild) <* (first + (secondchild-1 ))) secondchild --; // place a large child on the parent node * (first + holeindex) = * (first + secondchild); holeindex = secondchild; // secondchild = 2 * (secondchild + 1);} // No Right child, only left child, only need to be maintained once, because left child is the last element of heap if (secondchild = Len) {* (first + holeindex) = * (first + (secondchild-1); holeindex = secondchild-1;} // place the adjusted value in a proper position, equivalent to * (first + holeindex) = value; _ push_heap (first, holeindex, topindex, value);} // maintains the heaptemplate <class randomaccessiterator, class T, class distance> inline void _ pop_heap (randomaccessiterator first, randomaccessiterator last, randomacces Siterator result, T value, distance *) {* result = * First; // place the value at the top of the heap to the end of the heap, the value at the end of heap is stored in the parameter value: _ 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_heap (first, last-1, last-1, T (* (last-1), distance_type (first);/* according to the order of implicit representation heap, after pop The result is the first element of the container. Place the last element in the container header. Therefore, the maintenance interval is [first last-1) during maintenance ). * // Extract the heap top element and maintain the heaptemplate <class randomaccessiterator> inline void pop_heap (randomaccessiterator first, last) according to the max heap attribute {_ pop_heap_aux (first, last, last, value_type (first);} // maintains the heap downward. The comparison function comptemplate <class randomaccessiterator, class distance, class T, class compare> void _ adjust_heap (randomaccessiterator first, distance holeindex, distance Len, T value, compare comp) {Distance topindex = holeindex; distance secondchild = 2 * holeindex + 2; while (secondchild <Len) {If (COMP (* (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, comp);} // gets the top heap element and allows defining the comparison function comptemplate <class randomaccessiterator, class T, class compare, class distance> inline void _ pop_heap (randomaccessiterator first, randomaccessiterator last, random result, T value, compare comp, distance *) {* result = * First; _ adjust_heap (first, distance (0), distance (last-first), value, comp);} template <Class Identifier, 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);} // retrieve the heap top element and adjust heaptemplate <class randomaccessiterator according to comp, 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; // maintenance starts from the middle of heap. One-time maintenance [parent, parent-1 ,......, 0] While (true) {_ adjust_heap (first, parent, Len, T (* (first + parent); If (parent = 0) return; parent --;} // arrange [first, last) into a heap. Template <class randomaccessiterator> inline void make_heap (randomaccessiterator first, 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 <2) return; Distance Len = last-first; // maintenance starts from the middle of heap. One-time maintenance [parent, parent-1 ,......, 0] distance parent = (LEN-2)/2; while (true) {_ adjust_heap (first, parent, Len, T (* (first + parent )), comp); If (parent = 0) return; parent -- ;}// [first, last) is arranged into a heap. Execution of the size comparison function 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);} // elements between two iterators in ascending order, the elements between the two iterators meet the heap nature. The template <class randomaccessiterator> void sort_heap (randomaccessiterator first, randomaccessiterator last) {/* pop_heap () function is to delete heap top To reduce the heap size by 1 and put the heap top element at the end. The result of sort is ascending. */While (last-first> 1) pop_heap (first, last --); // every second row of pop_heap (), the operation of the second row is switched out .} // Sort (ascending) The elements between two iterators. The elements between the two iterators already meet the heap nature. // Allow the specified comparison function comptemplate <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:



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.