简体   繁体   English

关于C ++中的make_heap算法

[英]about make_heap algorithm in C++

http://www.cplusplus.com/reference/algorithm/make_heap/ http://www.cplusplus.com/reference/algorithm/make_heap/

In this link. 在此链接。 it says: 它说:

Internally, a heap is a tree where each node links to values not greater than its own value. 在内部,堆是一棵树,其中每个节点链接到不大于其自身值的值。 In heaps generated by make_heap, the specific position of an element in the tree rather than being determined by memory-consuming links is determined by its absolute position in the sequence, with *first being always the highest value in the heap. 在由make_heap生成的堆中,树中元素的特定位置而不是由消耗内存的链接确定,而是由其在序列中的绝对位置确定的,* first始终是堆中的最高值。

about "is determined by its absolute positon in the sequence" . 关于“由序列中的绝对位置决定”。 I confused here. 我在这里感到困惑。 It also says "a heap is a tree where each node linkes to values not greater than its own value" 它还说:“堆是一棵树,其中每个节点链接到不大于其自身值的值”

Do those 2 sentence contradict? 那两个句子矛盾吗? SO confused here. 这里很困惑。 What exactly tree is for a heap in C++? C ++中堆的确切树是什么?

Wish any kind person can help me out Thanks a lot 希望任何善良的人都能帮助我

If you look at heap implementations you see the tree is implemented as an array. 如果查看堆实现,则会看到树是作为数组实现的。 You can find the values below a node at index i at indexes 2 * i+1 and 2 * i +2 . 您可以在索引i的节点2 * i+12 * i +2的节点下面找到值。 So it is a tree, where you can access the elements by their absolute position in the array. 因此它是一棵树,您可以在其中按元素在数组中的绝对位置对其进行访问。

What this says is that a heap has a typical tree like structure, where each 'parent' node is greater than or equal to the value of the 'child' node ("...where each node links to values not greater than its own value..."). 这表示堆具有典型的树状结构,其中每个“父”节点都大于或等于“子”节点的值(“ ...其中每个节点链接到的值不大于其自身的值”值...”)。

It then goes on to say that instead of using links (ie pointers in, say, a struct (like you would use for a linked list)), it uses in-place memory (otherwise known as an array - "...is determined by its absolute position in the sequence..."). 然后继续说,它使用了就地内存(否则称为数组,即“ ... is”,而不是使用链接(例如,结构中的指针(如您要用于链接列表的指针))。由其在序列中的绝对位置确定...”)。

*first is the first element (or the largest/smallest, depending on the comparator function) on the heap, and is always at the [0]th index of the array. * first是堆上的第一个元素(或最大/最小,取决于比较器功能),并且始终在数组的第[0]个索引处。 For each index i, the children are located at [2*i+1] and [2*i+2]. 对于每个索引i,子项位于[2 * i + 1]和[2 * i + 2]。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM