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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • SQL Server中的sp_executesql系统存储过程

    SQL Server中的sp_executesql系统存储过程sp_executesql语法 sp_executesql[@stmt=]stmt[    {,[@params=]N@parameter_name  data_type[,…n]}    {,[@param1=]value1[,…n]}] 参数[@stmt=]stmt 包含 Transact-SQL 语

    2022年5月21日
  • vector二维数组初始化赋值_vector实现二维数组的赋值

    vector二维数组初始化赋值_vector实现二维数组的赋值一。二维vector初始化1.采用构造函数vector&lt;vector&lt;int&gt;&gt;vec(10,vector&lt;int&gt;(8));//10行8列,全部初始化为零2.添加元素(每次添加一行)inta[]={1,2,3,4};vector&lt;int&gt;ivec(a,a+4);//数组初始化vector,见最下面(…

    2022年9月18日
  • 防止攻击服务器_iis部署网站无法通过ip访问

    防止攻击服务器_iis部署网站无法通过ip访问摘要:介绍了IIS服务器常见的攻击及几种常见防御方式,阐述了IIS服务器的攻击原理,针对IIS服务器的缺陷阐述了IIS的常用防御方式,同时结合实例具体实现方式。关键词:IIS;服务器攻击;服务器防御中图分类号:TP393            文献标识码:A0         引言  随着Internet的不断发展与普及,英特网上出现了越来越多的WEB服务器。人们通过WEB服

    2022年8月30日
  • mysql tinyint长度_mysql设置取值范围0到100

    mysql tinyint长度_mysql设置取值范围0到100Tinyint占用1字节的存储空间,即8位(bit)。那么Tinyint的取值范围怎么来的呢?我们先看无符号的情况。无符号的最小值即全部8位(bit)都为0,换算成十进制就是0,所以无符号的Tinyint的最小值为0.无符号的最大值即全部8bit都为1,11111111,换算成十进制就是255.这很好理解。有符号的Tinyint的取值范围是怎么来的呢?在计算机中,用最高位表示符号。0表示正,1表示…

    2022年9月16日
  • redisclient怎么用_redis go

    redisclient怎么用_redis gohttps://blog.csdn.net/feinifi/article/details/87857477

    2022年10月12日
  • oracle 存储过程

    oracle 存储过程

    2021年7月7日

发表回复

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

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