Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <vector>
  4. #include <iterator>
  5. using namespace std;
  6. /*
  7. * Class Declaration
  8. */
  9. class BinaryHeap
  10. {
  11. private:
  12. vector <int> heap;
  13. int left(int parent);
  14. int right(int parent);
  15. int parent(int child);
  16. void heapifyup(int index);
  17. void heapifydown(int index);
  18. public:
  19. BinaryHeap()
  20. {}
  21. void Insert(int element);
  22. void DeleteMin();
  23. int ExtractMin();
  24. void DisplayHeap();
  25. int Size();
  26. };
  27. /*
  28. * Return Heap Size
  29. */
  30. int BinaryHeap::Size()
  31. {
  32. return heap.size();
  33. }
  34.  
  35. /*
  36. * Insert Element into a Heap
  37. */
  38. void BinaryHeap::Insert(int element)
  39. {
  40. heap.push_back(element);
  41. heapifyup(heap.size() -1);
  42. }
  43. /*
  44. * Delete Minimum Element
  45. */
  46. void BinaryHeap::DeleteMin()
  47. {
  48. if (heap.size() == 0)
  49. {
  50. cout<<"Heap is Empty"<<endl;
  51. return;
  52. }
  53. heap[0] = heap.at(heap.size() - 1);
  54. heap.pop_back();
  55. heapifydown(0);
  56. cout<<"Element Deleted"<<endl;
  57. }
  58.  
  59. /*
  60. * Extract Minimum Element
  61. */
  62. int BinaryHeap::ExtractMin()
  63. {
  64. if (heap.size() == 0)
  65. {
  66. return -1;
  67. }
  68. else
  69. return heap.front();
  70. }
  71.  
  72. /*
  73. * Display Heap
  74. */
  75. void BinaryHeap::DisplayHeap()
  76. {
  77. vector <int>::iterator pos = heap.begin();
  78. cout<<"Heap --> ";
  79. while (pos != heap.end())
  80. {
  81. cout<<*pos<<" ";
  82. pos++;
  83. }
  84. cout<<endl;
  85. }
  86.  
  87. /*
  88. * Return Left Child
  89. */
  90. int BinaryHeap::left(int parent)
  91. {
  92. int l = 2 * parent + 1;
  93. if (l < heap.size())
  94. return l;
  95. else
  96. return -1;
  97. }
  98.  
  99. /*
  100. * Return Right Child
  101. */
  102. int BinaryHeap::right(int parent)
  103. {
  104. int r = 2 * parent + 2;
  105. if (r < heap.size())
  106. return r;
  107. else
  108. return -1;
  109. }
  110.  
  111. /*
  112. * Return Parent
  113. */
  114. int BinaryHeap::parent(int child)
  115. {
  116. int p = (child - 1)/2;
  117. if (child == 0)
  118. return -1;
  119. else
  120. return p;
  121. }
  122.  
  123. /*
  124. * Heapify- Maintain Heap Structure bottom up
  125. */
  126. void BinaryHeap::heapifyup(int in)
  127. {
  128. if (in >= 0 && parent(in) >= 0 && heap[parent(in)] > heap[in])
  129. {
  130. int temp = heap[in];
  131. heap[in] = heap[parent(in)];
  132. heap[parent(in)] = temp;
  133. heapifyup(parent(in));
  134. }
  135. }
  136.  
  137. /*
  138. * Heapify- Maintain Heap Structure top down
  139. */
  140. void BinaryHeap::heapifydown(int in)
  141. {
  142.  
  143. int child = left(in);
  144. int child1 = right(in);
  145. if (child >= 0 && child1 >= 0 && heap[child] > heap[child1])
  146. {
  147. child = child1;
  148. }
  149. if (child > 0 && heap[in] > heap[child])
  150. {
  151. int temp = heap[in];
  152. heap[in] = heap[child];
  153. heap[child] = temp;
  154. heapifydown(child);
  155. }
  156. }
  157.  
  158. /*
  159. * Main Contains Menu
  160. */
  161. int main()
  162. {
  163. BinaryHeap h;
  164. while (1)
  165. {
  166. cout<<"------------------"<<endl;
  167. cout<<"Operations on Heap"<<endl;
  168. cout<<"------------------"<<endl;
  169. cout<<"1.Insert Element"<<endl;
  170. cout<<"2.Delete Minimum Element"<<endl;
  171. cout<<"3.Extract Minimum Element"<<endl;
  172. cout<<"4.Print Heap"<<endl;
  173. cout<<"5.Exit"<<endl;
  174. int choice, element;
  175. cout<<"Enter your choice: ";
  176. cin>>choice;
  177. switch(choice)
  178. {
  179. case 1:
  180. cout<<"Enter the element to be inserted: ";
  181. cin>>element;
  182. h.Insert(element);
  183. break;
  184. case 2:
  185. h.DeleteMin();
  186. break;
  187. case 3:
  188. cout<<"Minimum Element: ";
  189. if (h.ExtractMin() == -1)
  190. {
  191. cout<<"Heap is Empty"<<endl;
  192. }
  193. else
  194. cout<<"Minimum Element: "<<h.ExtractMin()<<endl;
  195. break;
  196. case 4:
  197. cout<<"Displaying elements of Hwap: ";
  198. h.DisplayHeap();
  199. break;
  200. case 5:
  201. exit(1);
  202. default:
  203. cout<<"Enter Correct Choice"<<endl;
  204. }
  205. }
  206. return 0;
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement