在线看毛片网站电影-亚洲国产欧美日韩精品一区二区三区,国产欧美乱夫不卡无乱码,国产精品欧美久久久天天影视,精品一区二区三区视频在线观看,亚洲国产精品人成乱码天天看,日韩久久久一区,91精品国产91免费

    
    
    <address id="vxupu"><td id="vxupu"></td></address>

      <pre id="vxupu"><small id="vxupu"></small></pre>
      <dfn id="vxupu"></dfn>
      <div id="vxupu"><small id="vxupu"></small></div>
    1. 新聞中心

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

      二叉堆的C語(yǔ)言實(shí)現(xiàn)

      作者: 時(shí)間:2016-12-01 來(lái)源:網(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://www.biyoush.com/article/201612/324511.htm

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

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

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

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

      /*實(shí)現(xiàn)數(shù)據(jù)的下慮法實(shí)現(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)存空間的值保存到第一個(gè)內(nèi)存空間中*/
      heap->parray[1] = heap->parray[heap->currentSize --];
      /*從表頭開(kāi)始將新的二叉樹(shù)進(jìn)行重新排序*/
      presort_BinaryHeap(heap, 1);

      return ret;
      }

      測(cè)試代碼:

      #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;
      }

      測(cè)試結(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


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


      上一頁(yè) 1 2 下一頁(yè)

      評(píng)論


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

      關(guān)閉