Chapter 5
Chapter 5
C+ + HE A PS
Contents
I. Lesson 5.1 – Heap
• Heap Data Structure
• The Height of a Heap
• Complete Binary Tree ADT
• A Vector Representation of Complete Binary Tree
void HeapPriorityQueue<E,C>::removeMin() {
T.removeLast(); // . . .remove it
else {
Position v = T.left(u);
if (T.hasRight(u) && isLess(*(T.right(u)), *v))
v = T.right(u); // v is u’s smaller child
if (isLess(*v, *u)) { // is u out of order?
T.swap(u, v); // . . .then swap
u = v;
}
else break; // else we’re done
}}}
Implementing Heap-Sort In-Place
• This performance is accomplished by modifying the algorithm as
follows:
1. We use a reverse comparator, which corresponds to a heap where the largest
element is at the top. At any time during the execution of the algorithm, we
use the left portion of L, up to a certain rank i−1, to store the elements in the
heap, and the right portion of L, from rank i to n−1 to store the elements in the
list. Thus, the first i elements of L (at ranks 0,...,i−1) provide the vector
representation of the heap (with modified level numbers starting at 0 instead
of 1), that is, the element at rank k is greater than or equal to its “children” at
ranks 2k+1 and 2k+2.
2. In the first phase of the algorithm, we start with an empty heap and
move the boundary between the heap and the list from left to right,
one step at a time. In step i (i = 1,...,n), we expand the heap by
adding the element at rank i−1 and perform up-heap bubbling.
3. In the second phase of the algorithm, we start with an empty list
and move the boundary between the heap and the list from right to
left, one step at a time. At step i (i = 1,...,n), we remove a maximum
element from the heap and store it at rank n−i.
• Figure 5.4.1 In-place heap-sort.
Parts (a) through (e) show the
addition of elements to the heap;
(f) through (j) show the removal
of successive elements. The
portions of the array that are used
for the heap structure are shown
in blue.
Bottom-Up Heap Construction
• Heap class instead of filling a heap using a series of n insert
operations. For simplicity, we describe this bottom-up heap
construction assuming the number n of keys is an integer of the type
n = 2h − 1. That is, the heap is a complete binary tree with every level
being full, so the heap has height h = log(n+1).
• Viewed nonrecursively, bottom-up heap construction consists of the
following h = log(n+1) steps:
1. In the first step (see Figure 5.4.2(a)), we construct (n+1)/2
elementary heaps storing one entry each.
2. In the second step (see Figure 5.4.2(b)–(c)), we form (n+ 1)/4 heaps,
each storing three entries, by joining pairs of elementary heaps and
adding a new entry. The new entry is placed at the root and may
have to be swapped with the entry stored at a child to preserve the
heap-order property.
3. In the third step (see Figure 5.4.2(d)–(e)), we form (n + 1)/8 heaps,
each storing 7 entries, by joining pairs of 3-entry heaps (constructed
in the previous step) and adding a new entry. The new entry is
placed initially at the root, but may have to move down with a
down-heap bubbling to preserve the heap-order property.
• Figure 5.4.2: Bottom-up
construction of a heap with 15
entries: (a) we begin by
constructing one-entry heaps on
the bottom level; (b) and (c) we
combine these heaps into three-
entry heaps; (d) and (e) seven-
entry heaps; (f) and (g) we create
the final heap. The paths of the
down-heap bubblings are
highlighted in blue. For simplicity,
we only show the key within each
node instead of the entire entry.