Ministry of Higher Education and Scientific Research University M’Hamed BOUGARA Boumerdes INSTITUTE OF ELECTRICAL AND ELECTRONIC ENGINEERING DEPARTEMENT OF ELECTRONICS Data Structures and Algorithms Lab Report 6 Binary Heap (priority queue) BERKANI Lina KHELIFI Cylia Grp : 02 Advisor: Dr. A. Zitouni BOUMERDES, JUNE 2023 Ministry of Higher Education and Scientific Research Institute of Electrical and Electronic Engineering Contents 1 Objectives 3 2 Apparatus 3 3 Application: Implementation of the Heap Sort Algorithm 4 4 Conclusion 6 Report type h Page 2/6 Ministry of Higher Education and Scientific Research Institute of Electrical and Electronic Engineering 1 Objectives Within this lab, we will make use of our Min-Heap that is implemented in C++ in implementing a Heap-Sort Algorithm. 2 Apparatus • GNU C++ Compiler (g++). • VS code. Report type h Page 3/6 Ministry of Higher Education and Scientific Research Institute of Electrical and Electronic Engineering 3 Application: Implementation of the Heap Sort Algorithm The main code makes use of the file ArrayMinHeap.h that exists in the same directory. The file contains the interface to our Min-Heap code: 1 #include <iostream> 2 3 using namespace std; 4 5 struct node { 6 int data; 7 int key; 8 }; 9 10 class ArrayMinHeap{ 11 public: 12 ArrayMinHeap(); // default constructor, init arr to NULL 13 ArrayMinHeap(int size); 14 ArrayMinHeap(node **array, int size); 15 ~ArrayMinHeap(); // destructor 17 void build(node **array, int size); void insert(node* &node); 18 void remove(); 19 int peek(); 20 void print(); 21 void sort(); 16 22 private: 23 node **arr; 24 int n; // n is the index of the last node 25 int size; 26 }; Report type h Page 4/6 Ministry of Higher Education and Scientific Research Institute of Electrical and Electronic Engineering The following code implements the Heap Sort Algorithm using the implemented Min-Heap in ArrayMinHeap.h. 1 #include "ArrayMinHeap.h" 2 3 int main(int argc, char const *argv[]) 4 { 5 6 node* a = new node; a->data = 0; a->key = 20; 7 node* b = new node; b->data = 0; b->key = 5; 8 node* c = new node; c->data = 0; c->key = 39; 9 node* d = new node; d->data = 0; d->key = 13; 10 node* e = new node; e->data = 0; e->key = 1; 11 node* f = new node; f->data = 0; f->key = 3; 12 node* g = new node; g->data = 0; g->key = 7; 13 node* h = new node; h->data = 0; h->key = 8; 14 node* r = new node; r->data = 0; r->key = 4; 15 16 node** array = new node*[9]; 17 18 array[0] = a; 19 array[1] = b; 20 array[2] = c; 21 array[3] = d; 22 array[4] = e; 23 array[5] = f; 24 array[6] = g; 25 array[7] = h; 26 array[8] = r; 27 28 ArrayMinHeap minHeap(array, 8); 29 minHeap.print(); 30 cout << "Prior element is: " << minHeap.peek() << endl; 31 cout << "----------------------------------" << endl; 32 minHeap.remove(); 34 cout << "Prior element deleted." << endl; minHeap.print(); 35 cout << "----------------------------------" << endl; 36 node* sixth = new node; sixth->data = 90; sixth->key = 3; 37 minHeap.insert(sixth); 38 cout << "New node added: (90, 3)" << endl; 39 minHeap.print(); 40 cout << "----------------------------------" << endl; 41 cout << "Sorted elements (non-increasing order): " << endl; 33 Report type h Page 5/6 Ministry of Higher Education and Scientific Research Institute of Electrical and Electronic Engineering 42 minHeap.sort(); 43 return 0; } 44 Running the code via the Command Prompt yields the following output: Figure 1: Output generated by the code of Heap Sort 4 Conclusion During this lab assignment, we learnt how to use Min Heaps for implementing Heap Sort Algorithm. Report type h Page 6/6