Implementing a minheap Write a program that user to perform
Solution
MinHeap.h
#include \"vector\"
 using namespace std;
class MinHeap
 {
 private:
 vector<int> HeapArray;
 void BubbleDown(int index);
 void BubbleUp(int index);
 void creat_minheap();
 int heapsize;
public:
 MinHeap(int* array, int length);
 MinHeap(const vector<int>& vector);
 MinHeap();
void Insert(int newValue);
 int GetMin();
 void DeleteMin();
void change_key(int k,int knew);
 void print();
 void print_content();
 };
=====================================================================
MinHeap.cpp
 #include \"MinHeap.h\"
MinHeap::MinHeap(int* array, int length) : HeapArray(length)
 {
 for(int i = 0; i < length; ++i)
 {
 HeapArray[i] = array[i];
 }
 heapsize=length;
creat_minheap();
 }
MinHeap::MinHeap(const vector<int>& vector) : HeapArray(vector)
 {
 creat_minheap();
 }
MinHeap::MinHeap()
 {
 }
void MinHeap::creat_minheap()
 {
 //int length = HeapArray.size();
 for(int i=heapsize-1; i>=0; --i)
 {
 BubbleDown(i);
 }
 }
void MinHeap::change_key(int k,int knew)
 {
 int pos = std::find(HeapArray.begin(), HeapArray.end(), k) - HeapArray.begin();
HeapArray[pos] =knew;
 creat_minheap();
 }
void MinHeap::BubbleDown(int index)
 {
 //int length = HeapArray.size();
 int leftChildIndex = 2*index + 1;
 int rightChildIndex = 2*index + 2;
if(leftChildIndex >= heapsize)
 return; //index is a leaf
int minIndex = index;
if(HeapArray[index] > HeapArray[leftChildIndex])
 {
 minIndex = leftChildIndex;
 }
   
 if((rightChildIndex < heapsize) && (HeapArray[minIndex] > HeapArray[rightChildIndex]))
 {
 minIndex = rightChildIndex;
 }
if(minIndex != index)
 {
   
 int temp = HeapArray[index];
 HeapArray[index] = HeapArray[minIndex];
 HeapArray[minIndex] = temp;
 BubbleDown(minIndex);
 }
 }
void MinHeap::BubbleUp(int index)
 {
 if(index == 0)
 return;
int parentIndex = (index-1)/2;
if(HeapArray[parentIndex] > HeapArray[index])
 {
 int temp = HeapArray[parentIndex];
 HeapArray[parentIndex] = HeapArray[index];
 HeapArray[index] = temp;
 BubbleUp(parentIndex);
 }
 }
void MinHeap::Insert(int newValue)
 {
 heapsize = HeapArray.size();
 HeapArray[heapsize] = newValue;
BubbleUp(heapsize);
 heapsize++;
 }
void MinHeap::print()
 {
 cout<<\"Current minimum is \"<<GetMin()<<\"\ \";
 cout<<\"Size of heap is \"<<HeapArray.size()<<\"\ \";
 }
void MinHeap::print_content()
 {int i;
 for( i=0; i<heapsize; ++i)
 {
 cout <<HeapArray[i] << \" \";
 
 }
 
 }
 int MinHeap::GetMin()
 {
 return HeapArray[0];
 }
   
 void MinHeap::DeleteMin()
 {
 heapsize = HeapArray.size();
if(heapsize == 0)
 {
 return;
 }
   
 HeapArray[0] = HeapArray[heapsize-1];
 HeapArray.pop_back();
 heapsize--;
BubbleDown(0);
 }
==============================================================
main.cpp
#include<bits/stdc++.h>
 #include \"MinHeap.cpp\"
 using namespace std;
 int main()
 {
 int array[] = {10, 4, 5, 30, 3, 300};
MinHeap *ptr;
   
int flag=1;
 MinHeap minHeap(array, 6);
   
 while(flag)
 {
 cout<<\"1)Create min heap\ 2)Delete minimum\ 3)Change key\ 4)Insert\ 5)Print\ 6)Print content\ 7)Exit\ \";
int ch;
 cin>>ch;
switch(ch)
 {
 case 1:
  
 ptr=&minHeap;
 break;
case 2:
 cout<<ptr->GetMin() <<\" is deleted\ \";
 ptr->DeleteMin();
 break;
 case 3:
 int k,k1;
 cout<<\"Enter old key\ \";
 cin>>k;
 cout<<\"Enter new key\ \";
 cin>>k1;
 ptr->change_key(k,k1);
 break;
case 4:cout<<\"Enter value to insert\ \";
 int v;
 cin>>v;
 ptr->Insert(v);
 cout<<\"Inserted\ \";
 break;
 case 5:
 ptr->print();
 break;
 case 6:ptr->print_content();
 break;
 case 7:flag=0;
 break;
 }
 }
return 0;
 }
==========================================================================
==============================================================================
akshay@akshay-Inspiron-3537:~/Chegg$ g++ main2.cpp
 akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
 1)Create min heap
 2)Delete minimum
 3)Change key
 4)Insert
 5)Print
 6)Print content
 7)Exit
 1
 1)Create min heap
 2)Delete minimum
 3)Change key
 4)Insert
 5)Print
 6)Print content
 7)Exit
 6
 3 4 5 30 10 300 1)Create min heap
 2)Delete minimum
 3)Change key
 4)Insert
 5)Print
 6)Print content
 7)Exit
 2
 3 is deleted
 1)Create min heap
 2)Delete minimum
 3)Change key
 4)Insert
 5)Print
 6)Print content
 7)Exit
 6
 4 10 5 30 300 1)Create min heap
 2)Delete minimum
 3)Change key
 4)Insert
 5)Print
 6)Print content
 7)Exit
 5
 Current minimum is 4
 Size of heap is 5
 1)Create min heap
 2)Delete minimum
 3)Change key
 4)Insert
 5)Print
 6)Print content
 7)Exit
 3
 Enter old key
 4
 Enter new key
 3
 1)Create min heap
 2)Delete minimum
 3)Change key
 4)Insert
 5)Print
 6)Print content
 7)Exit
 6
 3 10 5 30 300 1)Create min heap
 2)Delete minimum
 3)Change key
 4)Insert
 5)Print
 6)Print content
 7)Exit
 4
 Enter value to insert
 400
 Inserted
 1)Create min heap
 2)Delete minimum
 3)Change key
 4)Insert
 5)Print
 6)Print content
 7)Exit
 6
 3 10 5 30 300 400 1)Create min heap
 2)Delete minimum
 3)Change key
 4)Insert
 5)Print
 6)Print content
 7)Exit
 7






