简体   繁体   中英

about make_heap algorithm in C++

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.

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++?

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 . 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...").

*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. For each index i, the children are located at [2*i+1] and [2*i+2].

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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