A *heap* is a binary tree in which every node is larger than the values associated with either child. A heap and a binary tree, for that matter, can be very efficiently stored in a ** vector**, by placing the children of node

Using this encoding, the largest value in the heap is always located in the initial position, and can therefore be very efficiently retrieved. In addition, efficient logarithmic algorithms exist that permit a new element to be added to a heap and the largest element removed from a heap. For these reasons, a heap is a natural representation for the ** priority queue** datatype, described in Chapter 11.

The default operator is the less-than operator `<` appropriate to the element type. If desired, an alternative operator can be specified. For example, by using the greater-than operator `>`, one can construct a heap that locates the smallest element in the first location, instead of the largest.

The algorithm `std::make_heap()` takes a range, specified by random access ** iterator**s, and converts it into a heap. The number of steps required is a linear function of the number of elements in the range.

namespace std { void make_heap(RandomAccessIterator first, RandomAccessIterator last [, Compare ]); }

To add a new element to a heap, insert it at the end of a range using the `std::push_back()` member function of a ** vector** or

namespace std { void push_heap(RandomAccessIterator first, RandomAccessIterator last [, Compare ]); }

The algorithm `std::pop_heap()` swaps the first and final elements in a range, and restores to a heap the collection without the final element. The largest value of the original collection is therefore still available as the last element in the range. It can be accessed using the `back()` member function in a ** vector**, for example, and removed using the

namespace std { void pop_heap(RandomAccessIterator first, RandomAccessIterator last [, Compare ]); }

Finally, the algorithm `std::sort_heap()` converts a heap into an ordered or *sorted* collection. Note that a sorted collection is still a heap, although the reverse is not the case.

NOTE -- An ordered collection is a heap, but a heap need not necessarily be an ordered collection. In fact, a heap can be constructed in a sequence much more quickly than the sequence can be sorted.

The sort is performed using approximately `N log N` operations, where `N` represents the number of elements in the range. The `std::sort_heap()` algorithm is not stable. Equal elements are not guaranteed to retain their relative pre-sort order.

namespace std { void sort_heap(RandomAccessIterator first, RandomAccessIterator last [, Compare ]); }

Here is an example program that illustrates the use of these functions:

void heap_example() // illustrates the use of the heap algorithms // see alg7.cpp for complete source code { // make a heap of 15 random integers std::vector<int> aVec(15); std::generate(aVec.begin(), aVec.end(), randomValue); std::make_heap(aVec.begin(), aVec.end()); std::cout << "Largest value " << aVec.front() << std::endl; // remove largest and reheap std::pop_heap(aVec.begin(), aVec.end()); aVec.pop_back(); // add a 97 to the heap aVec.push_back (97); std::push_heap(aVec.begin(), aVec.end()); // finally, make into a sorted collection std::sort_heap(aVec.begin(), aVec.end()); }