The heap of basic data structures and the heap of data structures
-
Heap Definition
-
A heap is a complete binary tree or a similar full binary tree.
Heap nature
-
Heap Storage
-
Generally, the heap is represented by arrays. The subscript of the parent node of the I node is (I-1)/2. The subscript of its left and right subnodes is 2 * I + 1 and 2 * I + 2, respectively.
-
Heap features
-
Time complexity of inserting elements and popping up heap top elements lg (n)
-
Determine whether the sequence is heap
-
Judging by nature
-
For example, if the input sequence, is the root node, we can see that this is a small root heap.
15 30 22 93 52 71 89
Heap C language implementation
# Include <stdio. h> # include <stdlib. h> # include <string. h> # include <stdbool. h> // priority_queue in c ++ is a heap typedef int heap_elem; // data type of the heap/*** @ author Wei Xuan * @ time 2015/07/13 * @ basic structure of the brief heap **/typedef struct heap {int size; // actual number of elements int capacity; // heap_elem * elements; // Heap Storage array int (* cmp) (const heap_elem *, const heap_elem *); // element comparison function} heap;/*** @ author Wei Xuan * @ time 2015/07/13 * @ brief create a heap * @ param capacity * @ param cmp, if the value is less than-1, 0 is returned, and 1 is returned. In turn, the pointer of the heap object is returned successfully by @ return, otherwise nullptr **/heap * heap_create (const int capacity, int (* cmp) (const heap_elem *, const heap_elem *) {heap * myheap = (heap *) malloc (sizeof (heap); myheap-> size = 0; myheap-> capacity = capacity; myheap-> cmp = cmp; myheap-> elements = (heap_elem *) malloc (capacity * sizeof (heap_elem); return myheap ;} /*** @ author Wei Xuan * @ time 2015/07/13 * @ brief destroy heap * @ param * @ return none **/void heap_destory (heap * myheap) {free (myheap-> elements); free (myheap );} /*** @ author Wei Xuan * @ time 2015/07/13 * @ brief determines whether the heap is empty * @ param * @ return true indicates that null is returned, otherwise false **/bool heap_empty (const heap * myheap) {return myheap-> size = 0 ;} /*** @ author Wei Xuan * @ time 2015/07/13 * @ number of elements in the brief heap * @ param * @ return returns the number of elements **/int heap_size (const heap * myheap) {return myheap-> size ;} /*** @ author Wei Xuan * @ time 2015/07/13 * @ brief get the heap top element * @ param * @ return the heap top element **/heap_elem heap_top (const heap * myheap) {return myheap-> elements [0];} /*** @ author Wei Xuan * @ time 2015/07/13 * @ top-down Filtering Algorithm of brief's small root heap * @ node starting from param start * @ return **/void heap_sift_down (const heap * myheap, const int start) {int I = start; int j; const heap_elem temp = myheap-> elements [start]; for (j = 2 * I + 1; j <myheap-> size; j = 2 * j + 1) {if (j <myheap-> size-1 & myheap-> cmp (& (myheap-> elements [j]), & (myheap-> elements [j + 1])> 0) {j ++; // j points to the minor of the two children.} if (myheap-> cmp (& temp, & (myheap-> elements [j]) <= 0) {break ;} else {myheap-> elements [I] = myheap-> elements [j]; I = j ;}} myheap-> elements [I] = temp ;} /*** @ author Wei Xuan * @ time 2015/07/13 * @ brief small root heap bottom-up filtering algorithm * @ param start position * @ return **/void heap_sift_up (const heap * myheap, const int start) {int j = start; int I = (j-1)/2; const heap_elem temp = myheap-> elements [start]; while (j> 0) {if (myheap-> cmp (& (myheap-> elements [I]), & temp) <= 0) {break ;} else {myheap-> elements [j] = myheap-> elements [I]; j = I; I = (I-1)/2 ;}} myheap-> elements [j] = temp ;} /*** @ author Wei Xuan * @ time 2015/07/13 * @ brief add element * @ param * @ return **/void heap_push (heap * myheap, const heap_elem x) {// if the heap is full and the memory is re-allocated if (myheap-> size = myheap-> capacity) {heap_elem * temp = (heap_elem *) realloc (myheap-> elements, myheap-> capacity * 2 * sizeof (heap_elem); myheap-> elements = temp; myheap-> capacity * = 2 ;} myheap-> elements [myheap-> size] = x; myheap-> size ++; heap_sift_up (myheap, myheap-> size-1 );} /*** @ author Wei Xuan * @ time 2015/07/13 * @ brief pop-up top stack element * @ param * @ return **/void heap_pop (heap * myheap) {myheap-> elements [0] = myheap-> elements [myheap-> size-1]; myheap-> size --; heap_sift_down (myheap, 0 );} /*** @ author Wei Xuan * @ time 2015/07/13 * @ brief comparison of basic data types * @ param * @ return **/int cmp (const int * data1, const int * data2) {const int ret = * data1-* data2; if (ret <0) return-1; else if (ret> 0) return 1; else return 0;}/*** @ author Wei Xuan * @ time 2015/07/13 * @ brief print heap. The default value is int **/void heap_print (const heap * myheap) {if (myheap = NULL) return; for (int I = 0; I <myheap-> size; I ++) {printf_s ("% d \ t ", myheap-> elements [I]) ;}} int main () {heap * myheap = heap_create (6, cmp); heap_push (myheap, 10); heap_push (myheap, 3); heap_push (myheap, 2); heap_push (myheap, 5); heap_push (myheap, 1); puts ("initial... \ n "); heap_print (myheap); puts (" inserts 100, 20... \ n "); heap_push (myheap, 100); heap_push (myheap, 20); heap_print (myheap); puts (" Delete the heap top element... \ n "); heap_pop (myheap); heap_print (myheap); heap_destory (myheap); return 0 ;}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.