色婷婷AⅤ一区二区三区|亚洲精品第一国产综合亚AV|久久精品官方网视频|日本28视频香蕉

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > 二叉堆的C語言實現(xiàn)

          二叉堆的C語言實現(xiàn)

          作者: 時間:2016-12-01 來源:網(wǎng)絡(luò) 收藏

          ***********************************************************/
          static void presort_BinaryHeap(BinaryHeap_handle_t heap,int hole)
          {
          /*這是二叉堆的特性*/
          int child = hole * 2;
          /*保存當(dāng)前數(shù)據(jù)操作*/
          int tmp = 0;

          本文引用地址:http://cafeforensic.com/article/201612/324511.htm

          assert(heap != NULL && heap->parray != NULL);

          tmp = heap->parray[hole];
          /*hold * 2 <= heap->currentSize 判斷了當(dāng)前結(jié)點是否為最低層*/
          for(; hole * 2 <= heap->currentSize; hole = child)
          {
          child = hole * 2;

          /*******************************
          *這句代碼解決是否存在右結(jié)點的問題
          *并確定了那個子結(jié)點提升的問題
          *******************************/
          if((child != heap->currentSize)
          && (heap->parray[child + 1] < heap->parray[child]))
          child ++;

          if(heap->parray[child] < tmp)
          {
          /*將子結(jié)點提升為父結(jié)點*/
          heap->parray[hole] = heap->parray[child];
          }
          }
          /*到達(dá)了最低的層,也就是樹葉*/
          heap->parray[hole] = tmp;
          }

          /*實現(xiàn)數(shù)據(jù)的下慮法實現(xiàn)數(shù)據(jù)的刪除操作*/
          int deleteMin(BinaryHeap_handle_t heap)
          {
          int ret = 0;
          int index = 0;

          assert(!isEmpty(heap));
          /*需要返回的值*/
          ret = heap->parray[1];

          /*將最后需要釋放內(nèi)存空間的值保存到第一個內(nèi)存空間中*/
          heap->parray[1] = heap->parray[heap->currentSize --];
          /*從表頭開始將新的二叉樹進(jìn)行重新排序*/
          presort_BinaryHeap(heap, 1);

          return ret;
          }

          測試代碼:

          #include "binaryheap.h"
          #include
          #include

          void print_binaryheap(BinaryHeap_handle_t heap)
          {
          int i = 0;

          assert(heap != NULL && heap->parray != NULL);

          for(i = 1; i <= heap->currentSize; ++ i)
          {
          if(i %6)
          printf("%d ",heap->parray[i]);
          else
          printf("%d ",heap->parray[i]);
          }
          printf("");
          }

          int main()
          {
          int i = 0;
          int value = 0;

          srand((int)time(0));
          printf("********Test Binaryheap**************");

          BinaryHeap_t bheap;
          BinaryHeap_handle_t *pheap = NULL;

          printf("init and alloc test:");
          if(init_BinaryHeap(&bheap,10))
          {
          printf("init_BinaryHeap() successed!");
          }
          if (alloc_BinaryHeap(&pheap,15));
          {
          printf("alloc_BInaryHeap() successed!");
          }

          printf("***insert test*****");
          for(; i < 10; ++ i)
          {
          if(!insert(&bheap,5 * i - rand()%20))
          {
          printf("i = %d:insert failed !!",i);
          }
          }
          for(i = 0; i < 15; ++ i)
          {
          if(!insert(pheap,i * 8 - rand()%20))
          {
          printf("i = %d:insert failed!!",i);
          }
          }

          print_binaryheap(&bheap);
          print_binaryheap(pheap);

          printf("****deleteMin test****");
          for(i = 0; i < 5; ++ i)
          {
          value = deleteMin(&bheap);
          printf("bheap deleted:%d",value);
          value = deleteMin(pheap);
          printf("pheap deleted:%d",value);
          }
          print_binaryheap(&bheap);
          print_binaryheap(pheap);

          printf("deleteMin test successed");

          printf("****delete and free test:*******");
          delete_BinaryHeap(&bheap);

          printf("Is the bheap empty ? %s",
          isEmpty(&bheap)?"Yes":"No");

          free_BinaryHeap(&pheap);

          printf("*********Test successed!***********");
          pheap = NULL;
          return 0;
          }

          測試結(jié)果:

          [gong@Gong-Computer c_binaryheap]$ ./testbinaryheap
          ********Test Binaryheap**************
          init and alloc test:
          init_BinaryHeap()
          alloc_BInaryHeap()
          ***insert test*****
          -11 -9 -9 14 15
          10 21 23 40 26
          -16 2 14 20 13
          21 33 49 61 67 76
          86 83 95 109
          ****deleteMin test****
          bheap deleted:-11
          pheap deleted:-16
          bheap deleted:-9
          pheap deleted:2
          bheap deleted:-9
          pheap deleted:13
          bheap deleted:10
          pheap deleted:14
          bheap deleted:14
          pheap deleted:20
          15 23 21 40 26
          21 49 21 61 67
          76 33 95 83 109
          deleteMin test successed
          ****delete and free test:*******
          Is the bheap empty ? Yes
          *********Test


          從上面的測試結(jié)果可知,基本上實現(xiàn)了二叉堆的基本插入、刪除操作。代碼的關(guān)鍵點在于刪除中的下慮和插入過程中的上慮操作。以及如何判斷代碼是否存在右兒子,如何充分運用二叉堆的特性。


          上一頁 1 2 下一頁

          評論


          技術(shù)專區(qū)

          關(guān)閉