植物大戰 二叉樹 堆——純C

專注的阿熊發表於2022-04-25

#define _CRT_SECURE_NO_WARNINGS

#include "Heap.h"

//初始化結構體

void HeapInit(HP* php)

{

       assert(php);

       php->a = NULL;

       php->size = 0;

       php->capacity = 0;

}

//銷燬結構體

void HeapDestroy(HP* php)

{

       assert(php);

       free(php->a);

       php->a = NULL;

       php->size = 0;

       php->capacity = 0;

}

//交換

void HeapSwap(HPDataType* pa, HPDataType* pb)

{

       HPDataType tmp = *pa;

       *pa = *pb;

       *pb = tmp;

}

//向上調整

void AdjustUp(HPDataType* a, size_t child)

{

       size_t parent = (child - 1) / 2;

       while (child > 0)

       {

              if (a[child] < a[parent])

              {

                     HeapSwap(&a[child], &a[parent]);

                     child = parent;

                     parent = (child - 1) / 2;

              }

              else

              {

                     break;

              }

       }

}

//插入堆

void HeapPush(HP* php, HPDataType x)

{

       assert(php);

       if (php->size == php->capacity)

       {

              int newcapacity = php->capacity == 0 ? 4 : php->capacity * 2;

              HPDataType* tmp = (HPDataType*)realloc(php->a, sizeof(HPDataType)* newcapacity);

              if (tmp == NULL)

              {

                     printf("realloc fail\n");

                     exit(-1);

              }

              php->a = tmp;

              php->capacity = newcapacity;

       }

       php->a[php->size] = x;

       ++php->size;

       //向上調整

       AdjustUp(php->a, php->size - 1);

}

//向下調整

AdjustDown(HPDataType* a,size_t size, size_t root)

{

        size_t parent= root;

        size_t child= parent * 2 + 1;

        while (child < size)

        {

               if (child+1 < size && a[child + 1] < a[child])

               {

                      ++child;

               }

               if (a[child] < a[parent])

               {

                      HeapSwap(&a[parent], &a[child]);

                      parent = child;

                      child = parent * 2 + 1;

               }

               else

               {

                      break;

               }      

        }   

}

//刪除堆的值

void HeapPop(HP* php)

{

       assert(php);

       assert(php->size > 0);

       //交換堆頂和堆尾的值。然後刪除堆尾的值

       HeapSwap(php->a,外匯跟單gendan5.com &php->a[php->size - 1]);

       --php->size;

       //向下調整

       AdjustDown(php->a, php->size, 0);

}

//列印堆

void HeapPrint(HP* php)

{

       assert(php);

       for (size_t i = 0; i < php->size; ++i)

       {

              printf("%d ", php->a[i]);

       }

       printf("\n");

}

//判斷堆是否為空

bool HeapEmpty(HP* php)

{

       assert(php);

       return php->size == 0;

}

//求堆中有幾個元素

size_t HeapSize(HP* php)

{

       assert(php);

       return php->size;

}

//得到堆頂的值

HPDataType HeapTop(HP* php)

{

       assert(php);

       assert(php->size > 0);

       return php->a[0];

}


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2888542/,如需轉載,請註明出處,否則將追究法律責任。

相關文章