MIDDLE EAST TECHNICAL UNIVERSITY, NORTHERN CYPRUS CAMPUS
CNG315 Algorithms – In-class Exercises for Lecture 4
Topics: What is a heap?, Heapify Algorithm & Analysis, BuildHeap Algorithm & Analysis, HeapSort Algorithm
& Analysis, Heaps & Priority Queues.
1. Explain how the following tree satisfies the conditions to be a max-heap.
17
15 11
9 8 10 4
3 5 1
2. Show the array representation of the max-heap given in Q1.
3. Extract (or delete) the maximum element from the max-heap given in Q1. Show both the tree and array
representations.
4. Add 16 to the heap given in Q1. Show both the tree and array representations.
Page 1 of 2
5. Use {7, 20, 18, 15, 5, 25} to create a max-heap by using the BuildHeap operation. Show both the tree and
array representations.
6. Use the max-heap created in Q5 and apply the HeapSort algorithm. Show both the tree and array
representations.
Page 2 of 2