Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. int getMin(vector<int> &heap) {
  2.     if(heap.empty())
  3.         printf("Are you kidding?\n");
  4.     else return heap[0];
  5. }
  6.  
  7. void Insert(vector<int> &heap, int x) {
  8.     heap.push_back(x);
  9.     int loc = heap.size()-1;
  10.     while(loc){
  11.         if(heap[loc] < heap[loc-1>>1])
  12.             swap(heap[loc], heap[loc-1>>1]);
  13.         loc = loc-1>>1;
  14.     }
  15. }
  16.  
  17. void deleteRoot(vector<int> &heap) {
  18.     swap(heap[0], heap[heap.size()-1]);
  19.     heap.pop_back();
  20.     int pos = 0;
  21.     while(pos < heap.size()) {
  22.         int l, r, Min;
  23.         l = (2*pos+1 >= heap.size()) ? INF : heap[2*pos+1];
  24.         r = (2*pos+2 >= heap.size()) ? INF : heap[2*pos+2];
  25.         if(l == INF && r == INF) break;
  26.         Min = (l < r) ? 2*pos+1 : 2*pos+2;
  27.         if(heap[Min] < heap[pos])
  28.             swap(heap[pos], heap[Min]), pos = Min;
  29.         else break;
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement