Reference book "Data structure and algorithm analysis--c language description"
For some basic concepts about heaps, see another blog post for the Little ZZ.
/* This routine implements the minimum heap, the maximum heap is similar to/* #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include < string.h> #define MAX 13typedef struct bheap{int capacity;//heap maximum capacity int size;//Array Implementation of the current heap size int *bh;//heap}BHEAP,*BH ; BH init_bh (int Max);//initialization of the heap bool Isfull (BH h);//Determine whether the heap is full void insert (int data,bh h);//insert operation int deletmin (BH h);//delete min. bool IsEmpty (BH h);//determine if NULL void Printheap (BH h); int main () {int i=0; int data[11]={0,14,19,26,65,32,31,21,68,16,13}; BH Heap; HEAP=INIT_BH (MAX); for (i=1;i<=10;i++) {Insert (data[i],heap); } printheap (HEAP); Deletmin (HEAP); Printheap (HEAP); return 0;} BH init_bh (int Max) {BH H; H=malloc (sizeof (BHEAP)); if (h==null) printf ("Out of space!\n"); H->bh=malloc (sizeof (int) * (max+1)); memset (h->bh,0,sizeof (int) * (max+1)); if (h->bh==null) printf ("Out of space!\n"); h->capacity=max; h->size=0; h->bh[0]=0; return H;} BOOL Isfull (BH H) {if (h->capacity==h->size) Return true;//full else return false;//not full}void Insert (int data,bh H) {int i; if (Isfull (H)) {printf ("Binary Heap is full!\n"); Return } for (i=++h->size; H->BH[I/2]>DATA;I=I/2)//on filter {h->bh[i]=h->bh[i/2]; } H->bh[i]=data;} BOOL IsEmpty (BH H) {if (h->size==0) return true;//empty heap else return false;} int deletmin (BH H) {int i,child; int min,last; if (IsEmpty (H)) {printf ("Heap is empty!\n"); Return h->bh[0]; } min=h->bh[1]; last=h->bh[h->size--]; for (I=1;i*2<=h->size;i=child) {child=i*2;//left son if ((child
Data structure Learning-two-fork-heap ADT (programmatic)