Lecture 11 - Heap
Lecture 11 - Heap
February 2, 2025
+ 3
Heap Data Structure
February 2, 2025
+ 4
MinHeap and non-MinHeap examples
Violates MinHeap
13 property 21>6
13
21 16 21 16
24 31 19 68 6 31 19 68
65 26 32 A 65 26 32 Not a
MinHeap Heap
13 Not a 13
Heap Not a
21 16 21 Heap
16
24 31 19 68 24 31 19
65 26 32 65 26 32
Violates heap structural Violates heap structural
property property February 2, 2025
+ 5
MaxHeap and non-MaxHeap examples
Violates MaxHeap
68 68
property 65 < 67
65 46 65 46
24 32 23 25 67 32 23 25
15 20 31 A MaxHeap 15 20 31 Not a
Heap
70 Not a 30
Not a
Heap
50 Heap
40 21 16
24 31 19 38 19 18 10
15 20 25 2 5 15
Violates heap structural Violates heap structural
property property
February 2, 2025
+ Array Representation of a Binary 6
Heap
A heap is a dynamic data structure that is represented and
manipulated more efficiently using an array.
Since a heap is a balanced binary tree, its node values can be stored
in an array, without any gaps, in a breadth-first order, where:
Value(node )
i+1 array[ i ], for i > 0
13
21 16
24 31 19 68
32 26 65 68 19 31 24 16 21 13
9 8 7 6 5 4 3 2 1 0
65 26 32
• The root is array[0]
• The parent of array[i] is array[(i – 1)/2], where i > 0
• The left child, if any, of array[i] is array[2i+1].
• The right child, if any, of array[i] is array[2i+2].
February 2, 2025
+ Array Representation of a Binary Heap 7
(contd.)
We shall use an implementation in which the heap elements
are stored in an array starting at index 1.
Value(node i ) array[i] , for i > 1
13
21 16
24 31 19 68
10
32 26 65 68 19 31 24 16 21 13
65 26 32
9 8 7 6 5 4 3 2 1 0
• The root is array[1].
• The parent of array[i] is array[i/2], where i > 1
• The left child, if any, of array[i] is array[2i].
• The right child, if any, of array[i] is array[2i+1].
February 2, 2025
+ 8
Max Heap Insertion Algorithm
Step 1 − Create a new node at the end of heap.
Step 3 − Compare the value of this child node with its parent.
February 2, 2025
+ 9
MinHeap Insertion Example
13 13
21 16 Insert 18 21 16
24 31 19 68 24 31 19 68
65 26 32 65 26 32 18
Percolate up
13 13
18 16 21 16
Percolate up
24 21 19 68 24 18 19 68
65 26 32 31 65 26 32 31
February 2, 2025
+ 10
Max Heap Deletion Algorithm
Step 1 − Remove root node.
Step 3 − Compare the value of this child node with its parent.
Step 4 − If value of parent is less than child, then swap them. (SWAP with
the min child if minheap and max child if Maxheap)
February 2, 2025
+ MinHeap Dequeue Example 11
13 Delete min 31
element
18 19 delete last 18 19
node
24 21 23 68 24 21 23 68
65 26 32 31 65 26 32
Replace by value of last Percolate down
node
18 18
21 19 31 19
Percolate down
24 31 23 68 24 21 23 68
65 26 32 65 26 32 February 2, 2025
+ Deleting an arbitrary key
The algorithm of deleting an arbitrary key from a heap is:
12
Copy the key x of the last node to the node containing the deleted key.
Example:
February 2, 2025
+ Building a heap (top down) 13
A heap is built top-down by inserting one key at a time in an initially
empty heap.
After each key insertion, if the heap property is violated, it is restored
by percolating the inserted key upward.
read key;
binaryHeap.enqueue(key);
February 2, 2025
+ Heap Applications 14
February 2, 2025
+ 15
Teamwork practice
February 2, 2025
+ 16
Activity 1
Ifwe implement heap as min-heap, deleting root node (value
1)from the heap. What would be the value of root node after
second iteration if leaf node (value 100) is chosen to replace
the root at start.
a) 2
b) 100
c) 17
d) 3
February 2, 2025
+ 17
Activity 2
February 2, 2025
+ 18
Activity 3
Construct the heap binary tree from the following
array of elements.
13 14 15 18 11 12 17 16
February 2, 2025
+ 19
References
https://www.tutorialspoint.com/data_structures_algorithms/heap_data_structure.h
tm
https://www.geeksforgeeks.org/binary-heap/
Interactive page: https://visualgo.net/en/heap?slide=1
https://www.cs.cmu.edu/~adamchik/15-121/lectures/Binary%20Heaps/heaps.htm
l
Chapter 9 of the course textbook
February 2, 2025
+ 20
End of Heap
February 2, 2025