Chapter 7: Priority Queues - II

7.3 Heaps

7.3.1 The Heap Data Structure

Proposition 7.5: A heap T storing n keys has height h = [log(n + 1)].
Proof: (we apply the complete binary tree property)


7.3.2 Implementing a Priority Queue with a Heap

The Vector Representation of a Heap

Insertion





Up-Heap Bubbling after an Insertion



Removal


Down-Heap Bubbling after a Removal



Analysis

Function
Time
size(), isEmpty()
O(1)
minElement(), minKey()
O(1)
insertItem(k,e)
O(log n)
removeMin()
O(log n)

7.3.3 C++ Implementation

html-7.7 (HeapTree)
html-7.8 (HPQ1)
html-7.9 (HPQ2)

VisoAlgo

Heap Visualization

7.3.4 Heap-Sort
Heap Sort