最大(小)堆初始化,插入,删除,及利用其排序实现

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define esp 1e-8
//#define lson   l, m, rt<<1
//#define rson   m+1, r, rt<<1|1
#define sz(x) ((int)((x).size()))
#define pf(x) ((x)*(x))
#define pb push_back
#define pi acos(-1.0)
#define in freopen("solve_in.txt", "r", stdin);
#define out freopen("solve_out.txt", "w", stdout);
#define bug(x) cerr << "Line : " << (x) <<  " >>>>>>
";
#define TL cerr << "Time elapsed: " << (double)clock() / CLOCKS_PER_SEC * 1000 << " ms" << endl;
#define inf 0x0f0f0f0f

using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef map<LL, int> MPS;
typedef pair<int, int> PII;
typedef MPS::iterator IT;
class maxHeap {
public:
    vector<int> heap;
    int n;
    void init(vector<int> arr) {
        this->n = sz(arr);
        heap.resize(n+1);//heap[1...n]存放元素;
        for(int index = 1; index <= n; index++) {
            heap[index] = arr[index-1];
        }
        int id = n;
        while(id > 1) {
            heapDown(id/2);
            id -= 2;
        }
    }
    void heapDown(int rt) {
//        cout << rt << endl;
        if(rt > n) return;
        int lson = rt<<1;
        int rson = rt<<1|1;
        if(lson > n) return;
        if(rson > n || heap[rson] < heap[lson]) {
            if(heap[rt] < heap[lson]) {
                swap(heap[rt], heap[lson]);
                heapDown(lson);
            }
        } else  {
           if(heap[rt] < heap[rson]){
            swap(heap[rt], heap[rson]);
            heapDown(rson);
           }
        }
    }
    void heapUp(int rt) {
        while(rt > 1 && heap[rt] > heap[rt/2]) {
            swap(heap[rt], heap[rt/2]);
            rt /= 2;
        }
    }
    void insertHeap(int value) {
        heap.pb(value);
        n++;
        heapUp(n);
    }
    void deleteHeap(int pos) {
        if(pos == n) {
            heap.pop_back();
            n--;
        } else {
            swap(heap[pos], heap[n]);
            heap.pop_back();
            n--;
            if(pos == 1 || heap[pos] < heap[pos/2]) {
                heapDown(pos);
            } else {
                heapUp(pos);
            }
        }
    }
    int getMaxValue() {
        if(n >= 1) return heap[1];
        return -inf;
    }
    vector<int> getSortedList() {
        maxHeap tmpHeap = *this;
        vector<int> ans;
        while(tmpHeap.n) {
            ans.pb(tmpHeap.getMaxValue());
            tmpHeap.deleteHeap(1);
        }
        return ans;
    }
    void print() {
        vector<int> vec = getSortedList();
        for(int i = 0; i < sz(vec); i++)
            printf("%d%c", vec[i], i != sz(vec)-1 ? ' ' : '
');
    }
    void printHeap() {
        for(int i = 1; i <= n; i++)
            printf("%d%c", heap[i], i != n ? ' ' : '
');
    }
} solver;
int main() {
    in
    int n;
    vector<int> vec;
    cin >> n;
    for(int i = 0; i < n; i++) {
        int u;
        scanf("%d", &u);
//        cout << u << endl;
        vec.pb(u);
    }
    solver.init(vec);
    solver.printHeap();
    solver.insertHeap(80);
    solver.printHeap();
    solver.deleteHeap(2);
    solver.printHeap();
    solver.deleteHeap(2);
    solver.printHeap();
    solver.print();

    return 0;
}
View Code
原文地址:https://www.cnblogs.com/rootial/p/4308734.html