学习一下stl_heap

下面的算法是根据stl源码重新整合一下,是为了方便理解

因为使用的迭代器,为了在给定的迭代器之间使用heap的一些方法,

内部通常用disHole来确定某个节点 dishole 是指与first距离,可取0 1 2 3 4.....

inline void push_heap(int heap[], int first, int last)
{
//disHole 指 当前结点 到first的距离 因此距离 是从 0 1 2 3 .....
int disHole = (last - first) - ;
int disHoleParent = (disHole - ) / ;
int value = heap[first+disHole];
while (disHole> && heap[first + disHoleParent] < value)
{
heap[first+disHole] = heap[first+disHoleParent];
disHole = disHoleParent;
disHoleParent = (disHole - ) / ;
}
heap[first + disHole] = value;
} //调用此方法时 实际上将 最顶 元素 放到了最后
//从上往下调整
inline void pop_heap(int heap[], int first, int last)
{
int value = heap[last - ];
heap[last - ] = heap[first];
int disHole = first-first; int disSecondHoleChild = (disHole * + );
while (disSecondHoleChild < (last - first - ))
{
if (heap[first + disSecondHoleChild] < heap[first + disSecondHoleChild - ])
disSecondHoleChild--;
heap[first+disHole] = heap[first+disSecondHoleChild];
disHole = disSecondHoleChild;
disSecondHoleChild = (disHole * + );
}
if (disSecondHoleChild == (last - first - ))
{
heap[first + disHole] = heap[first + disSecondHoleChild - ];
disHole = disSecondHoleChild - ;
} heap[first+disHole] = value;
//这一步,因为在while 循环中,并没有设置让当前大于子女节点时退时
//所以插入后,使用push_head 进行向上调整
//当然暂时不理解stl在设计时 为何在while中不进行判断 break
push_heap(heap, first, disHole + );
}
//利用pop_heap 原理实现 排序
inline void sort_heap(int heap[],int first,int last)
{
while((last-first)>)
{
pop_heap(heap, first, last--);
}
}
//make_heap 将其调整为最大堆
inline void make_heap(int heap[],int first,int last)
{
if ((last - first) < )return;
int dislen = last - first - ;
int disParent = (dislen - ) / ;
while(true)
{
push_heap(heap, first, first + disParent + );
if(disParent==)return;
disParent--;
}
}

stl_heap的更多相关文章

  1. 《STL源代码分析》---stl_heap.h读书笔记

    Heap堆的数据结构是经常使用,Heap它还能够存储元件的.但STL并且不提供Heap集装箱.仅仅提供信息Heap算术运算.只支持RandomAccessIterator该容器可以被用作Heap集装箱 ...

  2. STL 源代码分析 算法 stl_heap.h

    本文senlie原版的.转载请保留此地址:http://blog.csdn.net/zhengsenlie heap ----------------------------------------- ...

  3. stl_heap.h

    stl_heap.h // Filename: stl_heap.h // Comment By: 凝霜 // E-mail: mdl2009@vip.qq.com // Blog: http://b ...

  4. C++ STL源代码学习(map,set内部heap篇)

    stl_heap.h ///STL中使用的是大顶堆 /// Heap-manipulation functions: push_heap, pop_heap, make_heap, sort_heap ...

  5. STL源代码分析——STL算法sort排序算法

    前言 因为在前文的<STL算法剖析>中,源代码剖析许多,不方便学习,也不方便以后复习.这里把这些算法进行归类,对他们单独的源代码剖析进行解说.本文介绍的STL算法中的sort排序算法,SG ...

  6. stl源码剖析 详细学习笔记 算法总览

    //****************************基本算法***************************** /* stl算法总览,不在stl标准规格的sgi专属算法,都以 *加以标 ...

  7. (转)c++一些知识点

    异常详解: https://www.cnblogs.com/hdk1993/p/4357541.html#top 模版详解: https://blog.csdn.net/lezardfu/articl ...

  8. STL源代码剖析——STL算法stl_algo.h

    前言 在前面的博文中剖析了STL的数值算法.基本算法和set集合算法.本文剖析STL其它的算法,比如排序算法.合并算法.查找算法等等.在剖析的时候.会针对函数给出一些样例说明函数的使用.源代码出自SG ...

  9. 论C++STL源代码中关于堆算法的那些事

    关于堆,我们肯定熟知的就是它排序的时间复杂度在几个排序算法里面算是比較靠上的O(nlogn)常常会拿来和高速排序和归并排序讨论,并且它还有个长处是它的空间复杂度为O(1), 可是STL中没有给我们提供 ...

随机推荐

  1. Virtual DOM 真的比操作原生 DOM 快吗?

    附上尤大的回答链接链接:https://www.zhihu.com/question/31809713/answer/53544875

  2. easy tornado

    easy tornado 题目分析 这是一道2018年护网杯的题目 /flag.txt /welcome.txt /hints.txt 一共有3个文件. /flag.txt flag in /flll ...

  3. 数据可视化之分析篇(三)Power BI总计行错误,这个技巧一定要掌握

    https://zhuanlan.zhihu.com/p/102567707 ​前一段介绍过一个客户购买频次统计的案例: Power BI 数据分析应用:客户购买频次分布. 我并没有在文章中显示总计行 ...

  4. [Cordova-IOS]JavaScript与Swift交互

    [Cordova-IOS]Swift调用JavaScript中的函数 概述 Cordova中,通过插件的形式可以实现JavaScript与Swift的交互,关于Cordova插件的定义以及Swfit如 ...

  5. [ArcEngine二次开发]为Feature的属性赋值

    在创建FeatureClass之后,需要为FeatureClass添加Features,在为Feature的字段赋值时,代码大致如下: 在这里赋值的时候,出现了一个错误: The operation ...

  6. GPO - Set Date and Time for Updates

    For Windows Update, the limitation normally is a time window, disk space, network bandwidth. Create ...

  7. OSCP Learning Notes - Exploit(1)

    Gaining Root with Metasploit Platform: Kali Linux, Kioptrix Level 1 1. Find the IP of Kioptirx nmap ...

  8. 题解 SP1841 【PPATH - Prime Path】

    模拟赛考到了这个题,但我傻傻的用了\(DFS\),于是爆了零 后来才想明白,因为搜索树的分支很多,但答案的深度却又没有那么深,所以在这里\(BFS\),而\(DFS\)一路搜到底的做法则会稳稳地\(T ...

  9. 2018年5月15日的sqlite安装和数据库记录

    sqlite数据库安装在d:\sqlite_files运行sqlite3查看数据表,命令,.tables 数据库文件 d:\sqlite_files\device.db create table de ...

  10. php 导出数据到excel类

    原文链接地址:http://www.oschina.net/code/snippet_212240_21885 标注:在使用时一定要屏蔽掉//$bodyVal = $this->charset( ...