STL中heap算法(堆算法)
来源:互联网 发布:js 中 json点不出来 编辑:程序博客网 时间:2024/04/30 20:28
①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--;
}
}
- STL中heap算法(堆算法)
- STL算法(Algorithms):堆(heap)
- 手写堆heap(STL的heap算法)
- STL算法 ---------- Heap算法
- 堆(Heap)排序算法
- 堆(heap)排序算法
- STL中的heap算法
- STL算法之heap
- C++ STL 算法:Heap算法
- STL算法---堆算法
- 【STL学习】堆相关算法详解与C++编程实现(Heap)
- 转【STL学习】堆相关算法详解与C++编程实现(Heap)
- 【算法学习】堆排序(Heap Sorting)
- 排序算法---堆排序(Heap Sort)
- 算法----堆排序(heap sort)
- 排序算法之堆(heap)
- STL——Heap算法
- STL 之堆算法
- Linux hostname命令修改主机名称
- VxWorks6.6 pcPentium BSP 使用说明(三):设备驱动
- VxWorks6.6 pcPentium BSP 使用说明(二):创建启动盘
- VxWorks6.6 pcPentium BSP 使用说明(一):基本概念
- Eeid—ligerUI学习(1)[首页布局]
- STL中heap算法(堆算法)
- cubieboard2 编译安装opencv
- this 关键字 (二)
- 实训一:电子商务模式
- 黑马程序员——正则表达式
- mingw编译thrif和boost
- 知己知彼,IT公司对数据挖掘相关从业者的的要求
- GPIO的驱动模型
- 一个简单木马分析及接管利用