DSA Lab 07
DSA Lab 07
Lab Session-07
Heap Data Structures and Priority Queues
Dr. M. Abbas Abbasi
1 Objective
Understanding heap data structures and their implementation as priority queues.
2 Learning Outcomes
After completing this lab session, students will be able to:
• Understand the concept and properties of binary heaps
• Implement min-heaps and max-heaps
• Create priority queues using heaps
• Apply basic heap operations (insertion, deletion, extraction)
• Analyze the time complexity of heap operations
1
4 Binary Max-Heap Implementation
1 # include < iostream >
2 # include < vector >
3 using namespace std ;
4
5 class MaxHeap {
6 private :
7 vector < int > heap ;
8
48 public :
49 MaxHeap () {}
50
2
62 int extractMax () {
63 if ( heap . empty () ) {
64 cout << " Heap is empty ! " << endl ;
65 return -1;
66 }
67
79 return max ;
80 }
81
122 cout << " Maximum element : " << maxHeap . getMax () << endl ;
123
124 cout << " Extracted max : " << maxHeap . extractMax () << endl ;
3
125 cout << " Max Heap after extraction : " ;
126 maxHeap . printHeap () ;
127
128 return 0;
129 }
Listing 1: Max-Heap Implementation
5 class MinHeap {
6 private :
7 vector < int > heap ;
8
18 if ( left < heap . size () && heap [ left ] < heap [ smallest ])
19 smallest = left ;
20
21 if ( right < heap . size () && heap [ right ] < heap [ smallest ])
22 smallest = right ;
23
24 if ( smallest != i ) {
25 swap ( heap [ i ] , heap [ smallest ]) ;
26 heapifyDown ( smallest ) ;
27 }
28 }
29
37 public :
38 MinHeap () {}
39
46 int extractMin () {
47 if ( heap . empty () ) {
48 cout << " Heap is empty ! " << endl ;
49 return -1;
50 }
51
4
53 heap [0] = heap . back () ;
54 heap . pop_back () ;
55
56 if (! heap . empty () )
57 heapifyDown (0) ;
58
59 return min ;
60 }
61
62 int getMin () {
63 if ( heap . empty () ) {
64 cout << " Heap is empty ! " << endl ;
65 return -1;
66 }
67 return heap [0];
68 }
69
70 bool isEmpty () {
71 return heap . empty () ;
72 }
73
74 int size () {
75 return heap . size () ;
76 }
77
78 void printHeap () {
79 for ( int i = 0; i < heap . size () ; i ++)
80 cout << heap [ i ] << " " ;
81 cout << endl ;
82 }
83 };
84
85 int main () {
86 MinHeap minHeap ;
87
97 cout << " Minimum element : " << minHeap . getMin () << endl ;
98
105 return 0;
106 }
Listing 2: Min-Heap Implementation
5
4
20 if ( left < heap . size () && heap [ left ]. second < heap [ smallest ]. second )
21 smallest = left ;
22
23 if ( right < heap . size () && heap [ right ]. second < heap [ smallest ]. second )
24 smallest = right ;
25
26 if ( smallest != i ) {
27 swap ( heap [ i ] , heap [ smallest ]) ;
28 heapifyDown ( smallest ) ;
29 }
30 }
31
39 public :
40 PriorityQueue () {}
41
60 if (! heap . empty () )
61 heapifyDown (0) ;
62
63 return value ;
64 }
65
6
67 int peek () {
68 if ( heap . empty () ) {
69 cout << " Priority Queue is empty ! " << endl ;
70 return -1;
71 }
72 return heap [0]. first ;
73 }
74
75 bool isEmpty () {
76 return heap . empty () ;
77 }
78
79 int size () {
80 return heap . size () ;
81 }
82
83 void display () {
84 cout << " Priority Queue contents ( value , priority ) : " << endl ;
85 for ( auto & item : heap ) {
86 cout << " ( " << item . first << " , " << item . second << " ) " ;
87 }
88 cout << endl ;
89 }
90 };
91
92 int main () {
93 PriorityQueue pq ;
94
101 pq . display () ;
102
103 cout << " Highest priority element : " << pq . peek () << endl ;
104
111 return 0;
112 }
Listing 3: Priority Queue Implementation
7 Lab Tasks
Task 1: Basic Heap Operations
Implement a min-heap supporting the following operations:
• Insert elements
• Extract the minimum element
• Decrease the value of a key at a given index
• Build a heap from an array
Test Cases:
7
1. Insert elements [10, 5, 15, 2, 20]
2. Extract min (should be 2)
3. Decrease the key at index 2 from 15 to 1
4. Extract min (should be 1)
5. Build a heap from [30, 10, 50, 5, 1]
6. Print the resulting min-heap