Binary heap Study Notes

Foreword

In fact, this thing studied for two years ...... so it should be reviewed his notes, right?

definition

Binary heap, short stack, by definition, is a binary tree is a complete binary tree. Notable characteristics is the size relationship between the value of the value of the child nodes entire tree parent nodes are the same (i.e., value of the parent node is greater than two child nodes value or less than the value of two child nodes). If it exceeds, large root called stack, the stack is less than the root is small. Obviously, the top element of the stack (i.e., the root node) as a binary heap maximum or minimum element. When stored, for convenience, we can store up to a whole binary array of 1-rooted subscript of an element are two sons (2i) and (2i + 1), father the subscript (i / 2).

use

Binary heap can be used to maintain a sequence of extrema. It supports the insertion of an element, delete, and query elements extremes extremes elements. By splicing two binary heap, we can also operate to delete the specified elements.

Operation implementation

insert

The first element is inserted into the end of a binary heap, and then compared against its parent node, the stack is not satisfied order to exchange it with its parent node, again until the entire binary heap binary heap to satisfy properties (i.e. reaches the top of the stack or the current comparison result satisfies the stack order).

void insert(const Type &x)
{
    a[++size]=x;
    unsigned int now=size;
    while(now>1&&!compare(a[now>>1],a[now]))
    {
        swap(a[now>>1],a[now]);
        now>>=1;
    }
    return;
}

Query top of the heap element

Return directly to the root node.

inline Type top(){return a[1];}

Delete the top of the heap element

First, the top of the stack element exchange places with the last element, and maintain the size of the heap (heap size minus one), the original top of the stack elements have been omitted. Next we want to maintain the nature of the heap, starting from the top of the heap element, keep the current element and small son (large root heap larger) are compared, if not meet the order of the exchange heap until the entire binary heap heap meet again properties (i.e., reaches the end or the current comparison result satisfies the stack order).

void erase()
{
    a[1]=a[size];
    --size;
    unsigned int now=1;
    while(now<<1<=size)
    {
        unsigned int t=now<<1;
        if((t|1)<=size&&compare(a[t|1],a[t]))
            t|=1;
        if(compare(a[now],a[t]))
            break;
        swap(a[now],a[t]);
        now=t;
    }
    return;
}

Delete the specified element (expand)

These are all common operating binary heap. But sometimes the problems we face involve removing elements of a particular value from a heap of them, which you can use two binary heap spliced ​​together to solve. We have established a secondary binary heap, binary heap to make it sort of a binary heap need to maintain the same. Obviously, when an element is not the original binary heap the top of the heap element, its presence or absence has no effect on the validity, so we can wait until then delete it when it becomes the top of the heap element. The auxiliary binary heap is used to store a sequence number has not yet been deleted. Each time delete operation is encountered, we first want to remove the element into the auxiliary binary heap. Wait until the operating elements take top of the heap, we continue to see if the original top of the heap element of a binary heap auxiliary binary heap equal, if the top of the heap and delete elements of the two binary heap until the original binary heap top of the heap binary elements of the auxiliary stack top of the stack elements are not equal. It is easy to prove, when an element to be top of the heap elements of the original binary heap, it is smaller than (large root heap large) elements have been removed, meaning larger than its elements do not exist on the secondary binary heap , so that needs to be removed only when the auxiliary binary heap stack top element is equal to the original binary heap, so the algorithm is correct.

inline Type top()
{
    while(!a.empty()&&!t.empty()&&a.top()==t.top())
    {
        a.erase();
        t.erase();
    }
    return a.top();
}
inline void insert(const Type &x){a.insert(x);return;}
inline void erase(const Type &x){t.insert(x);return;}

Total code

BasicHeap shall not delete the specified element with the basic operation of the heap, Heap heap to bring this action.

#ifndef _HEAP_HPP_
#define _HEAP_HPP_

template<typename Type>
class BasicHeap
{
    private:
        static const unsigned int maxn=500000;
        Type a[maxn+1];
        unsigned int size;
        bool (*compare)(const Type &a,const Type &b);
        static inline bool less(const Type &a,const Type &b){return a<b;}
        static inline void swap(Type &a,Type &b){a^=b;b^=a;a^=b;return;}
    public:
        BasicHeap(bool (*judge)(const Type &a,const Type &b)=less):size(0),compare(judge){}
        BasicHeap(const BasicHeap<Type> &b)
        {
            size=b.size;
            for(unsigned int i=1;i<=size;++i)
                a[i]=b.a[i];
        }
        inline Type top()const{return a[1];}
        inline bool empty()const{return size==0;}
        void insert(const Type &x)
        {
            a[++size]=x;
            unsigned int now=size;
            while(now>1&&!compare(a[now>>1],a[now]))
            {
                swap(a[now>>1],a[now]);
                now>>=1;
            }
            return;
        }
        void erase()
        {
            a[1]=a[size--];
            unsigned int now=1;
            while(now<<1<=size)
            {
                unsigned int t=now<<1;
                if((t|1)<=size&&compare(a[t|1],a[t]))
                    t|=1;
                if(compare(a[now],a[t]))
                    break;
                swap(a[now],a[t]);
            }
        }
};
template<typename Type>
class Heap
{
    private:
        BasicHeap<Type>a,t;
        static inline bool less(const Type &a,const Type &b){return a<b;}
    public:
        Heap(bool(*judge)(const Type &a,const Type &b)=less):a(judge),t(judge){}
        Heap(const Heap &b):a(b.a),t(b.t){}
        inline Type top()
        {
            while(!a.empty()&&!t.empty()&&a.top()==t.top())
            {
                a.erase();
                t.erase();
            }
            return a.top();
        }
        inline bool empty()const{return a.empty();}
        inline void insert(const Type &x){a.insert(x);return;}
        inline void erase(const Type &x){t.insert(x);return;}
};

#endif

Guess you like

Origin www.cnblogs.com/Psephurus-Gladius-zdx/p/12289214.html