Data structure Learning-two-fork-heap ADT (programmatic)

Source: Internet
Author: User

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-&GT;BH[I/2]&GT;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)

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.