【Topic】Heap

Introduction

The heap structure is an array object, which can be regarded as a complete binary tree (nodes except leaf nodes are full). The structure in the tree corresponds to the element that stores the value of the node in the array, as shown in the figure:

write picture description here

the nature of the heap


Let the length of the array A be len, the number of nodes in the binary tree is size, and size<=len, then A[i] stores the value of the node numbered i in the binary tree (1<=i<=size), and A[size] The following elements do not belong to the corresponding heap, the root of the tree is A[1], and using the properties of a complete binary tree, we can easily find the parent node (prarent(i)) and the left child node (left) of the i-th node. (i)), the subscripts of the right child node (right(i)) are: i/2, 2i, 2i+1.
More importantly, the heap has a property that for every node i except the root, A[parent(i)]>=A[i]. That is, except for the root node, the value of all nodes cannot exceed its parent node, so it is inferred that the largest element in the heap exists at the top of the heap, and the node value in the subtree of each node is equal to the value of the node, This kind of heap is also called "big root heap"; otherwise, it is called "small root heap".


heap operations

up and down
up is the process of moving a point up. As shown in the figure:
write picture description here
understand:
pascal

    procedure up(k:longint);
begin
        while(k>1)and(a[k]<a[k div 2])do
        begin
                a[0]:=a[k];
                a[k]:=a[k div 2];
                a[k div 2]:=a[0];
                k:=k div 2;
        end;
end;

c++

void up(int k)
{
    int now,next;
    a[++a_size]=k;
    now=a_size;
    while (now>1)
    {
        next=now>>1;
        if (a[now]>=a[next]) break;
        a[0]=a[k];
        a[k]=a[k/2];
        a[k/2]=a[0];
        now=next;
    }
}

C++ Standard Template Library STL

void up(int k)
{
    a[++a_size]=k;
    //push_a(a+1,a+a_size+1); //大根堆
    push_a(a+1,a+a_size+1,greater<int>());//小根堆
}

down is to move a point down. As shown below;
write picture description here
understand
pascal

procedure down(x:longint);
var
        y,k:longint;
begin
        while (x*2<=tot)and(a[x]>a[x*2])or(x*2+1<=tot)and(a[x]>a[x*2+1]) do
        begin
                y:=x*2;
                if(y+1<=tot)and(a[y]>a[y+1])then inc(y);
                k:=a[x];
                a[x]:=a[y];
                a[y]:=k;
                x:=y;
        end;
end;

c++

void down(int k)
{
    int now,next;
    now=k;
    while (now*2<=a_size)
    {
        next=now*2;
        if (next<a_size&&a[next+1]<a[next]) next++;
        if (a[now]<=a[next]) break;
        a[0]=a[now];
        a[now]=a[next];
        a[next]=a[0];
        now=nex;
    }
}

C++ Standard Template Library STL

void down()
{
    //pop_a(a+1,a+a_size+1);//大根堆
    pop_a(a+1,a+a_size+1,greater<int>());//小根堆  
}

In the application of heap, as long as these two programs are used properly, it will fly~~

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326047242&siteId=291194637