└── Implementation_of_heaps.c++ /Implementation_of_heaps.c++: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; class BHeap { 6 | private: 7 | vector heap; int l(int parent); int r(int parent); int par(int child); 8 | void heapifyup(int index); void heapifydown(int index); public: 9 | BHeap() {} 10 | void Insert(int element); void DeleteMin(); 11 | int ExtractMin(); void showHeap(); int Size(); 12 | }; 13 | 14 | int main() { 15 | 16 | BHeap h; while (1) { 17 | cout<<"1.Insert Element"<>c; 20 | switch(c) { case 1: 21 | cout<<"Enter the element to be inserted: "; cin>>e; 22 | h.Insert(e); break; 23 | case 2: h.DeleteMin(); 24 | break; case 3: 25 | if (h.ExtractMin() == -1) { cout<<"Heap is Empty"<::iterator pos = heap.begin(); cout<<"Heap --> "; 59 | while (pos != heap.end()) { cout<<*pos<<" "; pos++; 60 | } 61 | cout<= 0 && par(in) >= 0 && heap[par(in)] > heap[in]) { int temp = heap[in]; 82 | heap[in] = heap[par(in)]; heap[par(in)] = temp; heapifyup(par(in)); 83 | } 84 | } 85 | void BHeap::heapifydown(int in) { int child = l(in); 86 | int child1 = r(in); 87 | if (child >= 0 && child1 >= 0 && heap[child] > heap[child1]) { child = child1; 88 | } 89 | if (child > 0 && heap[in] > heap[child]) { int t = heap[in]; 90 | heap[in] = heap[child]; heap[child] = t; heapifydown(child); 91 | } 92 | } 93 | --------------------------------------------------------------------------------