Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int getMin(vector<int> &heap) {
- if(heap.empty())
- printf("Are you kidding?\n");
- else return heap[0];
- }
- void Insert(vector<int> &heap, int x) {
- heap.push_back(x);
- int loc = heap.size()-1;
- while(loc){
- if(heap[loc] < heap[loc-1>>1])
- swap(heap[loc], heap[loc-1>>1]);
- loc = loc-1>>1;
- }
- }
- void deleteRoot(vector<int> &heap) {
- swap(heap[0], heap[heap.size()-1]);
- heap.pop_back();
- int pos = 0;
- while(pos < heap.size()) {
- int l, r, Min;
- l = (2*pos+1 >= heap.size()) ? INF : heap[2*pos+1];
- r = (2*pos+2 >= heap.size()) ? INF : heap[2*pos+2];
- if(l == INF && r == INF) break;
- Min = (l < r) ? 2*pos+1 : 2*pos+2;
- if(heap[Min] < heap[pos])
- swap(heap[pos], heap[Min]), pos = Min;
- else break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement