Data Structures-heaps (heap)

Source: Internet
Author: User
Tags bool data structures int size min

The heap is also known as the priority queue. The operations allowed in the queue are FIFO, the element is inserted at the end of the team, and the elements are taken out of the team header. The same is true for heaps, where elements are inserted at the bottom of the heap, and elements are taken out of the heap, but the arrangement of elements in the heap is not in order of arrival, but in a certain order of precedence. This order of precedence can be the size of the element or other rules. As shown in Figure one is a heap, heap precedence is the large elements in front, small elements in the back, so that the resulting heap is called the maximum heap . The top element of the heap in the largest heap is the largest of the entire heap, and each branch can also be seen as a maximum heap. Similarly, we can define the minimum heap, as shown in Figure two.


Source: http://blog.qiji.tech/archives/4855 Heap Storage

A heap can be considered a binary tree, so you might consider using a two-tree representation to represent the heap. But because the elements in the heap are arranged in a certain order of precedence, it is possible to use an easier method- array -to represent it, which saves the child node pointer space and provides quick access to each node. The heap-count representation is actually the result of a heap-level traversal, as shown in the following figure:


Source: http://rock3.info/blog/category/algorithm-and-data-structures/

For each node labeled I, the left child node is labeled 2*i, and its right child node is labeled 2*i+1, and its parent node is labeled FLOOR{I/2}, where I starts with 1. Through the storage of the array, it is possible to obtain the relevant node directly by calculating the subscript.

typedef struct HEAP
{
    int capacity;
    int size;
    int *arr;
} Heap;

#define MIN-1

heap* createemptyheap (int max) {

    heap *heap = (heap*) malloc (sizeof (heap));

    if (heap = = NULL) {
        printf ("Out of space!\n");
        return NULL;
    } 

    heap->capacity = max;
    heap->size = 0;
    Heap->arr = (int*) malloc (sizeof (int) * (heap->capacity + 1));
    Heap->arr[0] = MIN;

    return heap;
}

Taking the minimum heap as an example, it shows that the heap is inserted, deleted and so on. Insert

The heap can also be seen as a complete binary tree, each time always filling up the first layer, and then inserting the next layer from left to right. Heap Insert Step: Adds a new element to the end of the heap in order of precedence, compares the new element to its parent node, and swaps the two locations if the new element is smaller than the parent node. Continue the 2nd step until you do not need to swap new elements and parent nodes, or reach the top of the heap by getting a minimum heap

The action of swapping the new element with the parent node is called a filter (percolate up).


Source: http://blog.csdn.net/tuke_tuke/article/details/50357939

heap* Insert (heap* heap, int val) {

    if (isfull (heap)) {
        printf ("Heap is full!\n");
        return heap;
    }

    Percolate up new element
    int i;
    for (i = ++heap->size; heap->arr[i] > val; I/= 2) 
        heap->arr[i] = heap->arr[i/2];
    Heap->arr[i] = val;

    return heap;
}


BOOL Isfull (heap* Heap) {

    return (heap->capacity = = heap->size);
}
Delete

The delete operation of the heap, in contrast to the insert operation, adjusts the heap from bottom to top, while the delete operation adjusts the heap from the top down. Delete the top element of the heap (usually the top of the heap is placed at the end of the array) to compare the left and right child nodes and raise the small elements. Continue with step 2 until you do not need to adjust or adjust to the bottom of the heap.

The above-mentioned adjustment method is called down filter (percolate down).

Source: http://blog.csdn.net/tuke_tuke/article/details/50357939

bool IsEmpty (heap* Heap) {return (Heap->size = = 0);}
        heap* deletemin (heap* heap) {if (IsEmpty (heap)) {printf ("Empty heap!\n");
    return NULL;
    } int minelement = heap->arr[1];

    int lastelement = heap->arr[heap->size--];
    int I, Childindex;

        for (i = 1; 2*i <= heap->size; i = childindex) {childindex = 2*i; Get the bigger child node if (Heap->arr[childindex]! = heap->size && Heap->arr[childi

        Ndex] > Heap->arr[childindex+1]) childindex++;
        if (Lastelement > Heap->arr[childindex]) heap->arr[i] = heap->arr[childindex];
    else break;

    } Heap->arr[i] = lastelement;
return heap; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.