当前位置: 移动技术网 > IT编程>开发语言>C/C++ > STL源码剖析——序列式容器#5 heap

STL源码剖析——序列式容器#5 heap

2019年11月08日  | 移动技术网IT编程  | 我要评论

  准确来讲,heap并不属于stl容器,但它是其中一个容器priority queue必不可少的一部分。顾名思义,priority queue就是优先级队列,允许用户以任何次序将任何元素加入容器内,但取出时是从优先权最高的元素开始取。而优先权有两种,可以是容器内数值最低的,也可以是数值最高的。而priority queue是选择了数值高作为评判优先级的标准。对应实现方法就是binary max heap,其作为priority queue的底层机制。

  所谓的binary heap(二叉堆),是一种完全二叉树,也就是说,整棵二叉树除了最底层的叶子节点外,其他节点都是被填满的,而最底层的节点是从左至右不得有空节点的,如图所示:

 

 

   如图所示,完全二叉树整棵树内没有任何节点漏洞,这使得我们可以使用数组来存储一棵完全二叉树上的所有结点。另外,如果我们数组的索引1开始记录节点,那么父节点与子节点在数组中的关系就一般为:父节点在 i 处,其左子节点必位于数组的 2i 处,其右子节点必位于数组的 2i+1 处。这种以数组表示树的方式,称为隐式表述法。我们的heap需要动态改变节点数,所以用vector是更好的选择。由于priority queue选择的是binary max heap做为自己的底层机制,所以也只提供了max heap的实现,所以我们接下里讨论的都是大根堆(max heap)。每个节点的键值都大于或等于其子节点的键值。

 

heap算法

  • push_heap

  为了满足完全二叉树的条件,最新加入的元素一定要放在最下一层做为叶子节点,并填补在从左至右的第一个空格。新加入的元素并不一定适合于现有的位置,为了满足max-heap的条件,我们需要对刚加入的元素进行一个上浮(percolate up)的操作:将新节点与其父节点比较,如果其键值比父节点的大,就父子交换位置,交换后新加的元素成为了父节点,此时再与它的父节点比较,如此一直上溯,直到不需对换或直到根节点为止。

 

 

   push_heap函数接受两个随机迭代器,用来表示一个heap底部容器(vector)的头尾,并且新元素已经插入到底部容器的最尾端,才会进入到该函数。如果参数不符合,或并无新元素插入,该函数的执行结果不可预期。

 1 template <class randomaccessiterator>
 2 inline void push_heap(randomaccessiterator first, randomaccessiterator last) {
 3     // 注意,此函数被调用时,新元素应已置于底层容器的最尾端。
 4     //即是新元素被加入到数组(原本最后元素的下一位置)后,才调用该函数
 5     __push_heap_aux(first, last, distance_type(first), value_type(first));
 6 }
7 template <class randomaccessiterator, class distance, class t> 8 inline void __push_heap_aux(randomaccessiterator first, 9 randomaccessiterator last, distance*, t*) { 10 __push_heap(first, distance((last - first) - 1), distance(0), 11 t(*(last - 1))); 12 //于上个函数获取到的迭代器所指对象类型t,用于强制转换 13 //distance(0)为指出最大值的索引值 14 //distance((last - first) - 1)为指出新添值的索引值 15 }
16 template <class randomaccessiterator, class distance, class t> 17 void __push_heap(randomaccessiterator first, distance holeindex, 18 distance topindex, t value) { 19 //holeindex为指出新添值的索引值 20 //topindex最大值的索引值 21 //value为新添值内容 22 distance parent = (holeindex - 1) / 2; // 找出新元素的父节点 23 while (holeindex > topindex && *(first + parent) < value) { 24 // 当尚未到达顶端,且父节点小于新值(不符合 heap 的次序特性),继续上浮 25 // 由于以上使用 operator<,可知 stl heap 是一种 max-heap(大根堆)。 26 *(first + holeindex) = *(first + parent); // 子位置设父值 27 holeindex = parent; // percolate up:調整新添值的索引值,向上提升至父节点。 28 parent = (holeindex - 1) / 2; // 获取新索引值的父节点 29 } // 持续到顶端,或满足 heap 的次序特性为止。 30 *(first + holeindex) = value; // 令最后的索引值为新值,完成插入。 31 }
  • pop_heap

  最大值在根节点处,pop操作取走根节点(其实是把它转移到vector的尾端节点上),为了满足完全二叉树的条件,必须割舍最底层最右边的节点,把其值拿出来放至一临时变量里,然后该位置放根节点的值(最后会被pop_back()给移除)。

 1 template <class randomaccessiterator>
 2 inline void pop_heap(randomaccessiterator first, randomaccessiterator last) {
 3     __pop_heap_aux(first, last, value_type(first));
 4 }
 5 
 6 template <class randomaccessiterator, class t>
 7 inline void __pop_heap_aux(randomaccessiterator first,
 8     randomaccessiterator last, t*) {
 9     __pop_heap(first, last - 1, last - 1, t(*(last - 1)), distance_type(first));
10     // 设定准条调整的值为尾值,然后将首值调至
11     // 尾节点(所以以上将迭代器 result 设为 last-1)。然后重整 [first, last-1),
12     // 使之重新成一個合格的 heap。
13 }
14 
15 template <class randomaccessiterator, class t, class distance>
16 inline void __pop_heap(randomaccessiterator first, randomaccessiterator last,
17     randomaccessiterator result, t value, distance*) {
18     *result = *first; // 设定尾值为首值,于是尾值即为要求的结果,
19                       // 可由客端稍后再调用 pop_back() 取出尾值。
20     __adjust_heap(first, distance(0), distance(last - first), value);
21     // 以上重新调整 heap,要调整的索引值为 0(亦即树根处),欲调整值为 value(原尾值)。
22 }
23 
24 template <class randomaccessiterator, class distance, class t>
25 void __adjust_heap(randomaccessiterator first, distance holeindex,
26     distance len, t value) {
27     //holeindex:要调整的索引值,从树根处出发
28     //len:所有元素个数(不包括被调整到尾节点的首节点),即[0, len)
29     //value:记录了原最底层最右边的节点的值
30     distance topindex = holeindex;
31     distance secondchild = 2 * holeindex + 2; // 调整的索引值的右子节点
32     while (secondchild < len) {
33         // 比较这左右两个子值,然后以 secondchild 代表较大子节点。
34         if (*(first + secondchild) < *(first + (secondchild - 1)))
35             secondchild--;
36         // percolate down:令较大子值代替要调整索引值处的值,再令调整的索引值下移至较大子节点处。
37         *(first + holeindex) = *(first + secondchild);
38         holeindex = secondchild;
39         // 继续找出要调整的索引值的右子节点
40         secondchild = 2 * (secondchild + 1);
41     }
42     if (secondchild == len) { // 相等,说明沒有右子节点(不是说没有,而是被准备pop掉的元素占用了,见*result = *first;),只有左子节点
43                               // percolate down:令左子值代替要调整索引值处的值,再令要调整的索引值下移至左子节点处。
44         *(first + holeindex) = *(first + (secondchild - 1));
45         holeindex = secondchild - 1;
46     }
47 
48     // 此时可能尚未满足次序特性,再执行一次上浮操作
49     __push_heap(first, holeindex, topindex, value);
50 }

   注意,pop_heap之后,最大元素只是被放置于底部容器的最尾端,尚未被取走。如果要取其值,可使用底部容器的back()函数。如果要移除它,可使用底部容器所提供的pop_back()函数。

  • sort_heap

  既然每次调用pop_heap可获得heap中键值最大的元素,如果持续对整个heap做pop_heap操作,且每次的操作范围都从后向前缩减一个元素,那么当整个程序执行完毕,我们便有了一个递增序列。sort_heap便是如此做的。该函数接受两个迭代器,用来表示heap的首尾。如果并非首尾,该函数的执行结果不可预期。注意,排序过后,底层容器里的就不再是一个合法的heap了。

1 template <class randomaccessiterator>
2 void sort_heap(randomaccessiterator first, randomaccessiterator last) {
3     // 以下,每执行一次 pop_heap(),极大值即被放在尾端。
4     // 尾端自减后(往左移动一格)再执行一次 pop_heap(),次极值又被放在新尾端。一直下去,最后即得
5     // 排序结果。
6     while (last - first > 1)
7         pop_heap(first, last--); // 每执行 pop_heap() 一次,操作范围即退缩一格。
8 }

 

  • make_heap

  这个算法用来将一段现有的数据转化为一个heap。把一段数据转换为heap的要点就是找到最后一个拥有子节点的节点,例如:

 

   假设这是一个普通数组,并无heap特性,那么要想其转化为一个heap,切入点就是找到最后一个拥有子节点的节点,上图而言就是e点,以e节点为首的子树,对该子树进行下沉操作和上浮操作,即先交换e跟j的值,再对j进行上浮操作。这样以e节点为首的子树就符合heap特性了。然后自减,来到了d节点(倒二拥有子节点的节点),同样对以d节点为首的子树进行下沉和上浮操作;然后再自减,直至到达根节点为止。这样整个数组就符合heap特性了。那么问题来了?怎么在一个普通数组中找到最后一个拥有子节点的节点的索引值呢?可以证明的是,如果有一长度为n的数组,那么最后一个拥有子节点的节点的索引值就是 (n - 2) / 2 ,这是数组从索引0开始的情况。

 1 // 将 [first,last) 排列为一个 heap。
 2 template <class randomaccessiterator>
 3 inline void make_heap(randomaccessiterator first, randomaccessiterator last) {
 4     __make_heap(first, last, value_type(first), distance_type(first));
 5 }
 6 
 7 template <class randomaccessiterator, class t, class distance>
 8 void __make_heap(randomaccessiterator first, randomaccessiterator last, t*,
 9     distance*) {
10     if (last - first < 2) return; // 如果長度為 0 或 1,不必重新排列。
11     distance len = last - first;
12     // 找出第一个需要重排的子树头部,以 parent 标示出。由于任何叶子节点都不需执行
13         // perlocate down(下沉),所以有以下计算。
14         distance parent = (len - 2) / 2;
15 
16     while (true) {
17         // 重排以 parent 为首的子树。len 是为了让 __adjust_heap() 判断操作范围
18         __adjust_heap(first, parent, len, t(*(first + parent)));
19         if (parent == 0) return; // 直至根节点,就结束。
20         parent--; // 未到根节点,就将(即将重排的子树的)索引值向前一個节点
21     }
22 }

  heap没有迭代器,heap的所有元素都必须遵循特别的排列规则,所以heap不提供遍历功能,也不提供迭代器。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网