c_cpp MaxHeap-的PriorityQueue

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp MaxHeap-的PriorityQueue相关的知识,希望对你有一定的参考价值。

#include<bits/stdc++.h>
using namespace std;

// #Heaps #PriorityQueues

class maxHeap{
private:
    vector<int> h;
public:
    // checks if heap is empty
    bool isEmpty(){
        if( h.size()==0 ){
            return true;
        }
        return false;
    }
    
    // gets parent index of current index
    int parent_index(int index){
        if(index <= 0){
            return -1; // top of heap has no parent
        }
        if( index&1 == 1){
        	// i odd
            // 2n+1 = left
            return (index-1)/2;
        }else{
        	// i even
            // 2n+2 =  right
            return (index-2)/2;
        }
    }
    
    // gets left child of current index
    int left_child_index(int index){
        int l = 2*index + 1;
        if( l < h.size() ){
            return l;
        }else{
            return -1;
        }
    }
    
    // gets right child of current index
    int right_child_index(int index){
        int r = 2*index + 2;
        if( r < h.size() ){
            return r;
        }else{
            return -1;
        }
    }
    
    // inserts a new element into the heap
    void insert(int value){
        h.push_back(value); // insert at bottom
        // swim up
        moveup(h.size() - 1);
    }
    
    // moves the current index towards top until max_heap
    // property is satisfied
    void moveup(int index){
        while(index > 0){
            int parentInd=parent_index(index);
            if(h[index] <= h[parentInd]){
                break; // exit condition
            }
            swap( h[parentInd], h[index] );
            index = parentInd;
        }
    }
    
    // moves elements downward RECURSIVELY until
    // max_heap property is satisfied
    void max_heapify(int index){
        int ind_of_largest = index;
        int l = left_child_index(index);
        int r = right_child_index(index);
        if(l != -1 && h[l] > h[ind_of_largest]){
            ind_of_largest = l;
        }
        if(r != -1 && h[r] > h[ind_of_largest]){
            ind_of_largest = r;
        }
        if(ind_of_largest == index){
            return; // base case ( no children || heap property satisfied)
        }
        swap(h[index], h[ind_of_largest]);
        max_heapify(ind_of_largest);
    }
    
    // changes value of an already existing key
    void change_key(int index, int value){
        int curr  = h[index];
        if(value >= curr){
            moveup(index);
        }else{
            max_heapify(index);
        }
    }
    
    // returns current max
    int peek_max(){
        if( !isEmpty() ){
            return h[0];
        }
        return INT_MAX;
    }
    
    // returns current max and removes it from the heap
    int extract_max(){
        if( isEmpty() ){
            return INT_MAX;
        }
        int curr_max = h[0];
        swap( h[0], h[h.size()-1] );
        h.pop_back();
        
        if( !isEmpty() ){
            max_heapify(0);
        }
        return curr_max;
    }
    
    // builds max_heap from bottom node recursively
    // converts a normal array into a max_heap
    void build_max_heap(){
        for(int i = ( h.size()-1 )/2; i>=0; i--){
            max_heapify(i);
        }
    }
    
    // prints heap
    void print_heap(){
        for(int i=0;i<h.size();i++){
            cout<<h[i]<<" ";
        }
        cout<<endl;
    }

};

int main(){
    maxHeap h1;
    h1.insert(2);
    h1.insert(78);
    h1.insert(6);
    h1.insert(9);
    h1.insert(8);
    h1.insert(66);
    h1.insert(52);
    h1.insert(48);
    h1.insert(36);
    h1.insert(92);
    h1.insert(18);
    h1.insert(16);
    h1.print_heap();
    cout<<h1.extract_max()<<endl;
    h1.print_heap();
    cout<<h1.extract_max()<<endl;
    h1.print_heap();
    cout<<h1.extract_max()<<endl;
    h1.print_heap();
    cout<<h1.extract_max()<<endl;
    h1.print_heap();
    return 0;
}

以上是关于c_cpp MaxHeap-的PriorityQueue的主要内容,如果未能解决你的问题,请参考以下文章

Python heapq 优先队列 Maxheap

maxHeap 的 python 实现

redis maxheap 51200000

构建 MaxHeap 最大没有被发送到顶部

数据结构c++语言描述——最大堆(MaxHeap)

数据结构笔记