码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-03-02 16:27:55      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:

 

技术分享
#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) <<  " >>>>>>\n";
#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 ?   : \n);
    }
    void printHeap() {
        for(int i = 1; i <= n; i++)
            printf("%d%c", heap[i], i != n ?   : \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

 

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

标签:

原文地址:http://www.cnblogs.com/rootial/p/4308734.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!