新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > 二叉堆的C語言實現

        二叉堆的C語言實現

        作者: 時間:2016-12-01 來源:網絡 收藏

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

        本文引用地址:http://www.104case.com/article/201612/324511.htm

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

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

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

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

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

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

        /*將最后需要釋放內存空間的值保存到第一個內存空間中*/
        heap->parray[1] = heap->parray[heap->currentSize --];
        /*從表頭開始將新的二叉樹進行重新排序*/
        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;
        }

        測試結果:

        [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


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


        上一頁 1 2 下一頁

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 鄂托克旗| 新建县| 和平县| 七台河市| 柘荣县| 布尔津县| 巴林右旗| 泾阳县| 黄梅县| 曲水县| 平阳县| 古浪县| 紫云| 平昌县| 会泽县| 阿勒泰市| 甘孜| 叶城县| 广平县| 黎城县| 卓资县| 湖口县| 宁城县| 太仆寺旗| 永济市| 托里县| 汉川市| 扬中市| 托克托县| 油尖旺区| 湘乡市| 河津市| 大冶市| 南皮县| 安化县| 峨边| 永清县| 孝昌县| 昭平县| 奉化市| 湖北省|