Insert/delete/print operations for heaps
#include <iostream>#include<vector>#include<math.h>using namespacestd;//the insertion of a heapvector<int> Insert (intx,vector<int>heap) { inttemp; if(Heap.empty ()) {heap.push_back (x); } Else{heap.push_back (x); for(intI=heap.size (); heap[i/2-1]>heap[i-1];i=i/2) {Temp=heap[i-1]; Heap[i-1]=heap[i/2-1]; Heap[i/2-1]=temp; if(i==0) Break; } } returnHeap;}//Delete the smallest element in the heapvector<int> Delete_element (vector<int>heap) {Vector<int>temp; Vector<int>heap_new; for(intI=1; I) Temp.push_back (Heap[i]); for(intj=0; J<temp.size (); j + +) {heap_new=Insert (temp[j],heap_new); } returnheap_new;}//printing of heap elementsvoidPrint_heap (vector<int>heap) { intCount=1; if(Heap.empty ()) cout<<"This heap is empty!"<<Endl; Else { for(intI=0; I) {cout<" "; if((i+2) ==pow (2, Count)) {cout<<Endl; Count++; } }} cout<<Endl;}intMain () {//Create an empty heapvector<int>Heap; intx; CharC; while(cin>>x) {heap=Insert (X,HEAP); C=cin.Get(); if(c=='\ n') Break; } print_heap (heap); Vector<int>HEAP2; HEAP2=delete_element (heap); Print_heap (HEAP2); return 0;}
Implementation of the priority queue (heap)