STL中heap算法(堆算法)

STL中heap算法(堆算法)



①push_heap算法
以下是push_heap算法的实现细节。该函数接收两个迭代器,用来表现一个heap底部容器(vector)的头尾,而且新元素已经插入究竟部的最尾端。
template <class RandomAccessIterator>
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>
inline void _push_heap_aux(RandomAccessIterator first,RandomAccessIterator last,
Distance*,T*)
{
 //以上系依据heap的结构特性:新值必置于底部容器的最尾端,此即第一个洞号:(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算法
pop操作取走根节点(事实上是设至底部容器vector的尾端节点)后,为了满足complete binary tree的条件,必须割舍最下层最右边的叶节点,并将其值又一次安插至最大堆。
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);
 //以上欲又一次调整heap,洞号为0(亦即树根处),欲调整值为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);
}

注意:pop_heap之后,最大元素仅仅是被置于底部容器的最尾端,尚未被取走。假设要取其值,可使用底部容器(vector)所提供的back()操作函数。假设要移除它,可使用底部容器(vector)所提供的pop_back()操作函数。
③sort_heap算法
既然每次pop_heap可获得heap中键值最大的元素,假设持续对整个heap做pop_heap操作,每次将操作范围从后向前缩减一个元素(由于pop_heap会把键值最大的元素放在底部容器的最尾端),当整个程序运行完成时,我们便有了一个递增序列。
template<class RandomAccessIterator>
void sort_heap(RandomAccessIterator first,RandomAccessIterator last)
{
 while(last – first > 1)
  pop_heap(first,last–);
}

④make_heap算法
这个算法用来将一段现有的数据转化为一个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–;
 }
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/119095.html原文链接:https://javaforall.net

(0)
上一篇 2021年11月16日 下午1:00
下一篇 2021年11月16日 下午2:00


相关推荐

  • 评分卡设计_创建绿色饭店的原则

    评分卡设计_创建绿色饭店的原则本文主要讲“变量选择”“模型开发”“评分卡创建和刻度”变量分析首先,需要确定变量之间是否存在共线性,若存在高度相关性,只需保存最稳定、预测能力最高的那个。需要通过VIF(varianceinflationfactor)也就是方差膨胀因子进行检验。变量分为连续变量和分类变量。在评分卡建模中,变量分箱(binning)是对连续变量离散化(discretization)的一种称呼

    2025年8月22日
    3
  • mysql新建连接1045_Navicat for Mysql 1045错误

    mysql新建连接1045_Navicat for Mysql 1045错误在使用图形用户工具NavicatforMySQL新建连接时,会报一个1045,某用户访问拒绝的错误。一般的解决办法是需要重新修改Mysql的密码,操作步骤如下:1netstopmysql停止mysql服务2进入mysql安装路径的bin目录下,使用mysqld命令,在cmd下mysqld–skip-grant-tables3开一个新的cmd窗口mysql不需要用户名就可…

    2022年5月8日
    79
  • 使用docker安装RocketMQ

    使用docker安装RocketMQ文章目录 1 拉取镜像 2 创建 namesrv 服务创建 namesrv 数据存储路径构建 namesrv 容器 3 创建 broker 节点创建 broker 数据存储路径创建配置文件构建 broker 容器安装 rocketmq 控制台拉取镜像构建 rocketmq 控制台服务容器需要关闭防火墙或者开放 namesrv 和 broker 端口关闭防火墙开放指定端口访问控制台 1 拉取镜像 dockerpullro rocketmq2 创建 namesrv 服务创建 namesrv 数据存储路径 mkdir p docke

    2026年3月18日
    2
  • poj1050最大子矩阵和

    poj1050最大子矩阵和

    2022年2月6日
    42
  • 女生学java软件开发怎么样?就业前景如何?

    女生学java软件开发怎么样?就业前景如何?学java目前现状是男生多于女生,从事java工作的也是男生多于女生,那么这种现状是说女生学java不好找工作吗?​  一、女生适合从事java吗?  在很多人的潜意识里,认为女生是不适合从事java工作的,因为他们觉得从事java工作的人逻辑性要相当的好,并且专业操作水平要高,而女生往往在这方面比较弱。其实这只是一种偏见,就像古代人觉得女子不该干涉朝政一样,女生也是适合从事java工作的,并且还能发挥自己的优势把java工作做得更好。  二、女生学java好找工作吗?  1.现在的女生

    2022年7月9日
    29
  • zTree实现地市县三级级联Service接口[通俗易懂]

    zTree实现地市县三级级联Service接口

    2022年2月6日
    40

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号