Practical application stack and heap

Practical application stack and heap

1. The concept of the stack
the stack memory can be regarded as an array of storage modification, however, the stack having a binary tree structure and stores the heap memory by type can be divided into small piles and stacks. In small pile (piles): any node key less than (greater than) equal to its left and right children of the key, the minimum is located in the top of the stack node key (maximum), from the root node to each node of the path sequence on the array elements are incremented (decremented) of.
The structure represents the size of the heap

2. Create a heap
heap it can be seen as simply an array transformed from. Thus, the stack may also create some conversion of
the first step, an array of elements inside a complete binary tree in layers by building a sort of way-index
second step ·, this binary tree by a downward adjustment method adjusting minimized heap
principle: start from the last adjustment of a non-leaf node has been up to the root node, each node will be adjusted to meet the nature of its sub-tree to a small heap.
Specific adjustment method:
1. Assuming the index of the node parent
2. The left-child node of the parent * = 2 + left. 1
3. If the right child right = parent * 2 + 1 is present, find the minimum among the left and right children
4. Compare the parent about the child is less than its smaller, less if the smaller end of the adjustment
or the parent of elements with small children exchange, if the adjustment is not satisfied after the small pile of nature, it would continue to adjust until subtree meet nature can be.
3. heap insertions and deletions
due to the special nature of the heap, so it insertions and deletions than the average binary tree to a lot of trouble
1. Heap Insert: insert a new element in the stack has been minimized behind completed, after insertion, after insertion, if the properties do not satisfy the tree node heap, need to be adjusted.
Methods
because they have a small pile, therefore, need to be adjusted only to the root node of the sub-tree from the leaves. First find the father of that node, if the node is less than the father, then you need to be adjusted upwards, after making adjustments to meet the small pile of nature, so repeated several times until the entire heap meet small pile of nature
2. heap Delete: Delete the heap is not lightly deleted, because not delete words completely destroy the small pile structure, it no longer has a small heap of nature, if you want to keep this property, we need to re built heap, then the time will be a great complexity.
The method
will substitute the last element of the stack top of the stack elements
because the number of elements in the stack is pressed standard control, so it only needs to be a stack index Save the last element to delete
this case, if the stack structure is destroyed, It requires only a simple downward adjustment can be small to meet the stack to re nature, and its time complexity is only log N, hence the stack is also possible to sort feasible.
Application heap

1. Priority Queue
ideas Analysis: Queuing priority queue can be used to illustrate this phenomenon is real. When you're going to line up to buy food, and if you go early, you can quickly buy, as well go late, then they would have at the back of others, you will late to buy, this time also need consider a problem if some people particularly slow, it would be considered a priority. Priority is characterized by a smaller number is set to a higher priority then it can be quickly accessed, it can process data more quickly and I put the code below.

void PriorityQueueInit(PriorityQueue* q)
{
    assert(q);
    q->_size=0;
    memset(q->_a,0,sizeof(DataType)*N);
}

void PriorityQueuePush(PriorityQueue* q, DataType x) 
{
    assert(q);
    if(q->_size>=N)
    {
    printf("PriorityQueuefull\n");
    return;
    }

    q->_a[q->_size++]=x;
     AdjustUp(q->_a,q->_size,q->_size-1);


}
void PriorityQueuePop(PriorityQueue* q)
{
    assert(q);
    if(NULL==q)
    {
        printf("PriorityQueueEmpty\n");
        return;
    }
    q->_a[0]=q->_a[q->_size-1];
    q->_size--;
    AdjustDown(q->_a,q->_size,0);





}
DataType PriorityQueueTop(PriorityQueue* q)
{
    assert(q);
    if(NULL==q)
    {
        printf("PriorityQueueEmpty\n");
        return;
    }

   return q->_a[0];
}
size_t PriorityQueueSize(PriorityQueue* q)
{
   assert(q);
   return q->_size;

}
size_t PriorityQueueEmpty(PriorityQueue* q)
{
    assert(q);
    return q->_size;
}
void HeapSort(DataType* a, size_t n)
{
    int i=0;
    assert(a);
    MakeHeap(a,n);



    while(n>0)
    {
    printf("%d ",a[0]);
    a[0]=a[n-1];
    n--;
    AdjustDown(a,n,0);
    }

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76

Find the maximum number of 210 million in the first k number (massive data topk problem)
ideas Analysis: At first glance, the number of 10 billion, very large indeed, a number total of four bytes, then the 10 billion number is required 40G of storage space, which is a regular computer is indeed impossible. However, this question certainly can not let us create a heap 10 billion data having no say so storage space is not big enough, the time complexity is great, right · practice is
1. Create a k elements the small pile
2. use the cycle, the last element and the first element to be replaced, and then adjusted downward to meet the small pile of nature, if this number are smaller than the root, then directly discarded, or adjusted until the date that some of the largest number of all the data inside the reactor are you looking for, so you can then print the
main code is as follows:

#include"Heap.h"
void MakeHeap(DataType* a, size_t n)//构建堆
{
    int i=(n-1)>>1;
    for(;i>=0;i--)
    {
        AdjustDown(a,n,i);
    }
}
void AdjustDown(DataType* a, size_t n, int root)//向下调整
{
    int parent =root;
    int child=2*parent+1;
    while(child<n)
    {
        if((child+1)<n&&a[child+1]<a[child])//右孩子存在且右孩子大于左孩子
        {
            ++child;//指向右孩子
        }
        if(a[child]<a[parent])
        {
            DataType tmp;
            tmp=a[child];
            a[child]=a[parent];
            a[parent]=tmp;

            parent=child;
            child=2*parent+1;
        }
        else
        {
            break;
        }

    }


}
void AdjustUp(DataType* a, size_t n, int child)//向上调整
{
    int parent=(child-1)>>1;
    while(child>0)
    {
        if(a[child]<a[parent])
        {
            DataType tmp;
            tmp=a[child];
            a[child]=a[parent];
            a[parent]=tmp;

            child=parent;
            parent=(child-1)>>1;
        }
        else
        {
            break;
        }
    }

}
void TopK(DataType* a, size_t n, size_t k)
{
    int i;
    MakeHeap(a,k);//建小堆
    for(i=k;i<n;i++)
    {
        a[0]=a[i];
        AdjustDown(a,k,0);

    }
    for(i=0;i<k;i++)
    {
        printf("%d ",a[i]);
    }

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75

Guess you like

Origin blog.csdn.net/mad_sword/article/details/93399526