• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            tbwshc

            tbw

              C++博客 :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
              95 Posts :: 8 Stories :: 3 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(4)

            我參與的團(tuán)隊(duì)

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

             二叉堆的實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)中如何使用,我任務(wù)主要是在操作系統(tǒng)中的任務(wù)優(yōu)先級調(diào)度問題,當(dāng)然也可以用于實(shí)現(xiàn)堆排序問題,比如找出數(shù)組中的第K個最小值問題,采用二叉堆能夠快速的實(shí)現(xiàn),今天我就采用C語言實(shí)現(xiàn)了一個簡單的二叉堆操作,完成這些數(shù)據(jù)結(jié)構(gòu)我并不知道能干什么,我就當(dāng)自己在練習(xí)C語言的功底吧。逐步完成自己的代碼,希望自己在知識的理解力上有一定的提高。

                二叉堆是非常有特點(diǎn)的數(shù)據(jù)結(jié)構(gòu),可以采用簡單的數(shù)組就能實(shí)現(xiàn),當(dāng)然鏈表的實(shí)現(xiàn)也是沒有問題的,畢竟是一個二叉樹問題,當(dāng)然可以采用鏈表實(shí)現(xiàn)。采用數(shù)組實(shí)現(xiàn)時(shí),可以找到兩個特別明顯的規(guī)律:

                左兒子:L_Son = Parent * 2;

                右兒子:R_Son = Parent * 2 + 1;

                二叉堆是一顆完全填滿的樹,可能例外的是在底層,底層上的元素是從左到右填入,當(dāng)然二叉堆可以是基于大值的排序,也可以是基于小值的排列形式,本文采用簡單的基于小值的形式。主要完成的操作:1、最小值的刪除操作,該操作會刪除根節(jié)點(diǎn),然后提升兒子節(jié)點(diǎn)來代替根節(jié)點(diǎn),具體的實(shí)現(xiàn)過程中通過提升左右兒子中較小的作為父結(jié)點(diǎn),依此提升直到到達(dá)最底層,這種實(shí)現(xiàn)方式叫做下慮法。2、數(shù)據(jù)的插入操作,插入操作可能會破壞二叉堆的結(jié)構(gòu),一般在最底層創(chuàng)建一個空穴,然后比較插入值與空穴父結(jié)點(diǎn)的值,如果大于父結(jié)點(diǎn)的值,那么直接插入到空穴中,如果小于父結(jié)點(diǎn),則將父結(jié)點(diǎn)的值插入到剛創(chuàng)建的空穴中,在父結(jié)點(diǎn)所在位置上形成新的父結(jié)點(diǎn),這時(shí)候再和父結(jié)點(diǎn)的父結(jié)點(diǎn)比較,具體操作如上所述,直到找到具體的插入地址。當(dāng)結(jié)點(diǎn)個數(shù)為偶數(shù)時(shí),在刪除操作中需要注意節(jié)點(diǎn)是否有右兒子的情況。具體的可以參考代碼中的說明。

                具體的實(shí)現(xiàn)如下:

                結(jié)構(gòu)體:

                #ifndef __BINARYHEAP_H_H_

                #define __BINARYHEAP_H_H_

                #include <stdlib.h>

                #include <assert.h>

                #define bool int

                #define true 1

                #define false 0

                /*打算采用數(shù)組的方式實(shí)現(xiàn)完全二叉堆*/

                typedef struct _binaryheap

                {

                /*因?yàn)樾枰獎討B(tài)擴(kuò)展,

                *采用靜態(tài)數(shù)組不方便*/

                int * parray;

                /*目前存在的結(jié)點(diǎn)*/

                int currentSize;

                /*樹的實(shí)際容量*/

                int capacity;

                }BinaryHeap_t, *BinaryHeap_handle_t;

                #ifdef __cplusplus

                extern "C"

                {

                #endif

                bool init_BinaryHeap(BinaryHeap_handle_t heap, int capacity);

                bool alloc_BinaryHeap(BinaryHeap_handle_t *heap, int capacity);

                void delete_BinaryHeap(BinaryHeap_handle_t heap);

                void free_BinaryHeap(BinaryHeap_handle_t *heap);

                bool insert(BinaryHeap_handle_t heap,int value);

                int deleteMin(BinaryHeap_handle_t heap);

                bool isEmpty(BinaryHeap_handle_t heap);

                #ifdef __cplusplus

                }

                #endif

                #endif

                實(shí)現(xiàn)的接口函數(shù)如下:

                #include "binaryheap.h"

                bool isEmpty(BinaryHeap_handle_t heap)

                {

                assert(heap != NULL);

                return heap->currentSize == 0;

                }

                bool init_BinaryHeap(BinaryHeap_handle_t heap, int capacity)

                {

                int *parray = NULL;

                if(heap == NULL)

                return false;

                parray = (int *)calloc(capacity+1,sizeof(int));

                if(parray == NULL)

                return false;

                heap->parray = parray;

                heap->capacity = capacity;

                heap->currentSize = 0;

                return true;

                }

                void delete_BinaryHeap(BinaryHeap_handle_t heap)

                {

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

                heap->capacity = 0;

                heap->currentSize = 0;

                free(heap->parray);

                heap->parray = NULL;

                }

                void free_BinaryHeap(BinaryHeap_handle_t *heap)

                {

                assert(*heap != NULL);

                (*heap)->capacity = 0;

                (*heap)->currentSize = 0;

                free((*heap)->parray);

                (*heap)->parray = NULL;

                free(*heap);

                *heap = NULL;

                }

                bool alloc_BinaryHeap(BinaryHeap_handle_t *heap, int capacity)

                {

                int *parray = NULL;

                if(*heap != NULL)

                return false;

                *heap = (int *)calloc(1, sizeof(BinaryHeap_t));

                if(*heap == NULL)

                return false;

                /*其中的1,主要是為了使得數(shù)組從下標(biāo)1開始計(jì)算*/

                parray =(int *)calloc(capacity + 1, sizeof(int));

                if(parray == NULL)

                return false;

                (*heap)->parray = parray;

                (*heap)->capacity = capacity;

                (*heap)->currentSize = 0;

                return true;

                }

                /**************************************************

                * 采用上慮法實(shí)現(xiàn)數(shù)據(jù)的插入操作

                * 上慮法的實(shí)現(xiàn)方式比較簡單,首先創(chuàng)建一個空節(jié)點(diǎn)

                * 然后將需要插入的值與當(dāng)前空穴的父結(jié)點(diǎn)進(jìn)行比較

                * 如果大于父結(jié)點(diǎn),直接插入空穴中

                * 如果小于父結(jié)點(diǎn)的值,則將父結(jié)點(diǎn)的值下拉到空穴中

                * 之前父結(jié)點(diǎn)的位置就是空穴,接著與上層比較

                * 直到找到父結(jié)點(diǎn)大于當(dāng)前插入值的情況

                **************************************************/

                bool insert(BinaryHeap_handle_t heap, int value)

                {

                int index = 0;

                if(heap == NULL || heap->parray == NULL)

                return false;

                /*得到一個新的空穴下標(biāo)*/

                index = ++heap->currentSize;

                /*條件是不是第一個下標(biāo)和插入值比對應(yīng)父結(jié)點(diǎn)小*/

                while(index > 1 && value < heap->parray[index/2])

                {

                /*將父結(jié)點(diǎn)保存到當(dāng)前結(jié)點(diǎn)處*/

                heap->parray[index] = heap->parray[index/2];

                /*得到父結(jié)點(diǎn)的空穴位置*/

                index /= 2;

                }

                /*將插入的值保存到剩余的空穴中*/

                heap->parray[index] = value;

                return true;

                }

                /***********************************************************

                * 下慮法實(shí)現(xiàn)數(shù)據(jù)的重排序操作

                * 實(shí)現(xiàn)的方式,將子結(jié)點(diǎn)的兩個兒子進(jìn)行比較,將小的提升

                * 需要注意的是如何讓判斷節(jié)點(diǎn)是否一定存在右兒子

                * 實(shí)現(xiàn)的方式主要是利用了二叉堆的特性:

                * 2*pare = L_child

                * 2*pare + 1 = R_child;

                ***********************************************************/

                static void presort_BinaryHeap(BinaryHeap_handle_t heap,int hole)

                {

                /*這是二叉堆的特性*/

                int child = hole * 2;

                /*保存當(dāng)前數(shù)據(jù)操作*/

                int tmp = 0;

                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)的問題

                *并確定了那個子結(jié)點(diǎ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á)了最低的層,也就是樹葉*/

                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)存空間的值保存到第一個內(nèi)存空間中*/

                heap->parray[1] = heap->parray[heap->currentSize --];

                /*從表頭開始將新的二叉樹進(jìn)行重新排序*/

                presort_BinaryHeap(heap, 1);

                return ret;

                }

                測試代碼:

                #include "binaryheap.h"

                #include <stdio.h>

                #include <time.h>

                void print_binaryheap(BinaryHeap_handle_t heap)[nettpage]

                {

                int i = 0;

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

                for(i = 1; i <= heap->currentSize; ++ i)

                {

                if(i %6)

                printf("%d\t",heap->parray[i]);

                else

                printf("\n%d\t",heap->parray[i]);

                }

                printf("\n");

                }

                int main()

                {

                int i = 0;

                int value = 0;

                srand((int)time(0));

                printf("********Test Binaryheap**************\n");

                BinaryHeap_t bheap;

                BinaryHeap_handle_t *pheap = NULL;

                printf("init and alloc test:\n");

                if(init_BinaryHeap(&bheap,10))

                {

                printf("init_BinaryHeap() successed!\n");

                }

                if (alloc_BinaryHeap(&pheap,15));

                {

                printf("alloc_BInaryHeap() successed!\n");

                }

                printf("***insert test*****\n");

                for(; i < 10; ++ i)

                {

                if(!insert(&bheap,5 * i - rand()%20))

                {

                printf("i = %d:insert failed !!\n",i);

                }

                }

                for(i = 0; i < 15; ++ i)

                {

                if(!insert(pheap,i * 8 - rand()%20))

                {

                printf("i = %d:insert failed!!\n",i);

                }

                }

                print_binaryheap(&bheap);

                print_binaryheap(pheap);

                printf("****deleteMin test****\n");

                for(i = 0; i < 5; ++ i)

                {

                value = deleteMin(&bheap);

                printf("bheap deleted:%d\n",value);

                value = deleteMin(pheap);

                printf("pheap deleted:%d\n",value);

                }

                print_binaryheap(&bheap);

                print_binaryheap(pheap);

                printf("deleteMin test successed\n");

                printf("****delete and free test:*******\n");

                delete_BinaryHeap(&bheap);

                printf("Is the bheap empty ? %s\n",

                isEmpty(&bheap)?"Yes":"No");

                free_BinaryHeap(&pheap);

                printf("*********Test successed!***********\n");

                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é)果可知,基本上實(shí)現(xiàn)了二叉堆的基本插入、刪除操作。代碼的關(guān)鍵點(diǎn)在于刪除中的下慮和插入過程中的上慮操作。以及如何判斷代碼是否存在右兒子,如何充分運(yùn)用二叉堆的特性。    二叉堆的實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)中如何使用,我任務(wù)主要是在操作系統(tǒng)中的任務(wù)優(yōu)先級調(diào)度問題,當(dāng)然也可以用于實(shí)現(xiàn)堆排序問題,比如找出數(shù)組中的第K個最小值問題,采用二叉堆能夠快速的實(shí)現(xiàn),今天我就采用C語言實(shí)現(xiàn)了一個簡單的二叉堆操作,完成這些數(shù)據(jù)結(jié)構(gòu)我并不知道能干什么,我就當(dāng)自己在練習(xí)C語言的功底吧。逐步完成自己的代碼,希望自己在知識的理解力上有一定的提高。

                二叉堆是非常有特點(diǎn)的數(shù)據(jù)結(jié)構(gòu),可以采用簡單的數(shù)組就能實(shí)現(xiàn),當(dāng)然鏈表的實(shí)現(xiàn)也是沒有問題的,畢竟是一個二叉樹問題,當(dāng)然可以采用鏈表實(shí)現(xiàn)。采用數(shù)組實(shí)現(xiàn)時(shí),可以找到兩個特別明顯的規(guī)律:

                左兒子:L_Son = Parent * 2;

                右兒子:R_Son = Parent * 2 + 1;

                二叉堆是一顆完全填滿的樹,可能例外的是在底層,底層上的元素是從左到右填入,當(dāng)然二叉堆可以是基于大值的排序,也可以是基于小值的排列形式,本文采用簡單的基于小值的形式。主要完成的操作:1、最小值的刪除操作,該操作會刪除根節(jié)點(diǎn),然后提升兒子節(jié)點(diǎn)來代替根節(jié)點(diǎn),具體的實(shí)現(xiàn)過程中通過提升左右兒子中較小的作為父結(jié)點(diǎn),依此提升直到到達(dá)最底層,這種實(shí)現(xiàn)方式叫做下慮法。2、數(shù)據(jù)的插入操作,插入操作可能會破壞二叉堆的結(jié)構(gòu),一般在最底層創(chuàng)建一個空穴,然后比較插入值與空穴父結(jié)點(diǎn)的值,如果大于父結(jié)點(diǎn)的值,那么直接插入到空穴中,如果小于父結(jié)點(diǎn),則將父結(jié)點(diǎn)的值插入到剛創(chuàng)建的空穴中,在父結(jié)點(diǎn)所在位置上形成新的父結(jié)點(diǎn),這時(shí)候再和父結(jié)點(diǎn)的父結(jié)點(diǎn)比較,具體操作如上所述,直到找到具體的插入地址。當(dāng)結(jié)點(diǎn)個數(shù)為偶數(shù)時(shí),在刪除操作中需要注意節(jié)點(diǎn)是否有右兒子的情況。具體的可以參考代碼中的說明。

                具體的實(shí)現(xiàn)如下:

                結(jié)構(gòu)體:

                #ifndef __BINARYHEAP_H_H_

                #define __BINARYHEAP_H_H_

                #include <stdlib.h>

                #include <assert.h>

                #define bool int

                #define true 1

                #define false 0

                /*打算采用數(shù)組的方式實(shí)現(xiàn)完全二叉堆*/

                typedef struct _binaryheap

                {

                /*因?yàn)樾枰獎討B(tài)擴(kuò)展,

                *采用靜態(tài)數(shù)組不方便*/

                int * parray;

                /*目前存在的結(jié)點(diǎn)*/

                int currentSize;

                /*樹的實(shí)際容量*/

                int capacity;

                }BinaryHeap_t, *BinaryHeap_handle_t;

                #ifdef __cplusplus

                extern "C"

                {

                #endif

                bool init_BinaryHeap(BinaryHeap_handle_t heap, int capacity);

                bool alloc_BinaryHeap(BinaryHeap_handle_t *heap, int capacity);

                void delete_BinaryHeap(BinaryHeap_handle_t heap);

                void free_BinaryHeap(BinaryHeap_handle_t *heap);

                bool insert(BinaryHeap_handle_t heap,int value);

                int deleteMin(BinaryHeap_handle_t heap);

                bool isEmpty(BinaryHeap_handle_t heap);

                #ifdef __cplusplus

                }

                #endif

                #endif

                實(shí)現(xiàn)的接口函數(shù)如下:

                #include "binaryheap.h"

                bool isEmpty(BinaryHeap_handle_t heap)

                {

                assert(heap != NULL);

                return heap->currentSize == 0;

                }

                bool init_BinaryHeap(BinaryHeap_handle_t heap, int capacity)

                {

                int *parray = NULL;

                if(heap == NULL)

                return false;

                parray = (int *)calloc(capacity+1,sizeof(int));

                if(parray == NULL)

                return false;

                heap->parray = parray;

                heap->capacity = capacity;

                heap->currentSize = 0;

                return true;

                }

                void delete_BinaryHeap(BinaryHeap_handle_t heap)

                {

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

                heap->capacity = 0;

                heap->currentSize = 0;

                free(heap->parray);

                heap->parray = NULL;

                }

                void free_BinaryHeap(BinaryHeap_handle_t *heap)

                {

                assert(*heap != NULL);

                (*heap)->capacity = 0;

                (*heap)->currentSize = 0;

                free((*heap)->parray);

                (*heap)->parray = NULL;

                free(*heap);

                *heap = NULL;

                }

                bool alloc_BinaryHeap(BinaryHeap_handle_t *heap, int capacity)

                {

                int *parray = NULL;

                if(*heap != NULL)

                return false;

                *heap = (int *)calloc(1, sizeof(BinaryHeap_t));

                if(*heap == NULL)

                return false;

                /*其中的1,主要是為了使得數(shù)組從下標(biāo)1開始計(jì)算*/

                parray =(int *)calloc(capacity + 1, sizeof(int));

                if(parray == NULL)

                return false;

                (*heap)->parray = parray;

                (*heap)->capacity = capacity;

                (*heap)->currentSize = 0;

                return true;

                }

                /**************************************************

                * 采用上慮法實(shí)現(xiàn)數(shù)據(jù)的插入操作

                * 上慮法的實(shí)現(xiàn)方式比較簡單,首先創(chuàng)建一個空節(jié)點(diǎn)

                * 然后將需要插入的值與當(dāng)前空穴的父結(jié)點(diǎn)進(jìn)行比較

                * 如果大于父結(jié)點(diǎn),直接插入空穴中

                * 如果小于父結(jié)點(diǎn)的值,則將父結(jié)點(diǎn)的值下拉到空穴中

                * 之前父結(jié)點(diǎn)的位置就是空穴,接著與上層比較

                * 直到找到父結(jié)點(diǎn)大于當(dāng)前插入值的情況

                **************************************************/

                bool insert(BinaryHeap_handle_t heap, int value)

                {

                int index = 0;

                if(heap == NULL || heap->parray == NULL)

                return false;

                /*得到一個新的空穴下標(biāo)*/

                index = ++heap->currentSize;

                /*條件是不是第一個下標(biāo)和插入值比對應(yīng)父結(jié)點(diǎn)小*/


            while(index > 1 && value < heap->parray[index/2])

                {

                /*將父結(jié)點(diǎn)保存到當(dāng)前結(jié)點(diǎn)處*/

                heap->parray[index] = heap->parray[index/2];

                /*得到父結(jié)點(diǎn)的空穴位置*/

                index /= 2;

                }

                /*將插入的值保存到剩余的空穴中*/

                heap->parray[index] = value;

                return true;

                }

                /***********************************************************

                * 下慮法實(shí)現(xiàn)數(shù)據(jù)的重排序操作

                * 實(shí)現(xiàn)的方式,將子結(jié)點(diǎn)的兩個兒子進(jìn)行比較tbw,將小的提升

                * 需要注意的是如何讓判斷節(jié)點(diǎn)是否一定存在右兒子

                * 實(shí)現(xiàn)的方式主要是利用了二叉堆的特性:

                * 2*pare = L_child

                * 2*pare + 1 = R_child;

                ***********************************************************/

                static void presort_BinaryHeap(BinaryHeap_handle_t heap,int hole)

                {

                /*這是二叉堆的特性*/

                int child = hole * 2;

                /*保存當(dāng)前數(shù)據(jù)操作*/

                int tmp = 0;

                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)的問題

                *并確定了那個子結(jié)點(diǎ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á)了最低的層,也就是樹葉*/

                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)存空間的值保存到第一個內(nèi)存空間中*/

                heap->parray[1] = heap->parray[heap->currentSize --];

                /*從表頭開始將新的二叉樹進(jìn)行重新排序*/

                presort_BinaryHeap(heap, 1);

                return ret;

                }

                測試代碼:

                #include "binaryheap.h"

                #include <stdio.h>

                #include <time.h>

                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\t",heap->parray[i]);

                else

                printf("\n%d\t",heap->parray[i]);

                }

                printf("\n");

                }

                int main()

                {

                int i = 0;

                int value = 0;

                srand((int)time(0));

                printf("********Test Binaryheap**************\n");

                BinaryHeap_t bheap;

                BinaryHeap_handle_t *pheap = NULL;

                printf("init and alloc test:\n");

                if(init_BinaryHeap(&bheap,10))

                {

                printf("init_BinaryHeap() successed!\n");

                }

                if (alloc_BinaryHeap(&pheap,15));

                {

                printf("alloc_BInaryHeap() successed!\n");

                }

                printf("***insert test*****\n");

                for(; i < 10; ++ i)

                {

                if(!insert(&bheap,5 * i - rand()%20))

                {

                printf("i = %d:insert failed !!\n",i);

                }

                }

                for(i = 0; i < 15; ++ i)

                {

                if(!insert(pheap,i * 8 - rand()%20))

                {

                printf("i = %d:insert failed!!\n",i);

                }

                }

                print_binaryheap(&bheap);

                print_binaryheap(pheap);

                printf("****deleteMin test****\n");

                for(i = 0; i < 5; ++ i)

                {

                value = deleteMin(&bheap);

                printf("bheap deleted:%d\n",value);

                value = deleteMin(pheap);

                printf("pheap deleted:%d\n",value);

                }

                print_binaryheap(&bheap);

                print_binaryheap(pheap);

                printf("deleteMin test successed\n");

                printf("****delete and free test:*******\n");

                delete_BinaryHeap(&bheap);

                printf("Is the bheap empty ? %s\n",

                isEmpty(&bheap)?"Yes":"No");

                free_BinaryHeap(&pheap);

                printf("*********Test successed!***********\n");

                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é)果可知,基本上實(shí)現(xiàn)了二叉堆的基本插入、刪除操作。tbw代碼的關(guān)鍵點(diǎn)在于刪除中的下慮和插入過程中的上慮操作。以及如何判斷代碼是否存在右兒子,如何充分運(yùn)用二叉堆的特性。

            posted on 2012-09-22 17:58 tbwshc 閱讀(341) 評論(0)  編輯 收藏 引用

            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            久久综合久久综合久久| 久久久www免费人成精品| 久久久久高潮毛片免费全部播放| 日韩久久久久中文字幕人妻| 伊人色综合久久天天| 亚洲国产成人久久综合碰碰动漫3d| WWW婷婷AV久久久影片| 人妻少妇久久中文字幕| 久久无码人妻一区二区三区午夜| 日韩人妻无码精品久久久不卡| 亚洲AV日韩精品久久久久| 久久久精品2019免费观看| 久久久久亚洲AV无码永不| 精品一区二区久久| 99久久精品国产毛片| 久久人人爽人人精品视频| 综合久久给合久久狠狠狠97色| 亚洲国产视频久久| 久久精品国产亚洲av麻豆小说| 一本一道久久综合狠狠老| 久久精品国产亚洲av影院| 久久精品成人免费观看97| 一本一本久久a久久精品综合麻豆| 久久久久久久97| 欧美精品一区二区精品久久| 四虎国产精品免费久久| 久久亚洲精品无码AV红樱桃| 欧美亚洲国产精品久久蜜芽| 四虎影视久久久免费观看| 亚洲精品乱码久久久久久中文字幕 | 久久最新精品国产| 亚洲精品国产自在久久| 无码人妻久久一区二区三区免费| 9久久9久久精品| 欧美久久久久久| 久久精品无码一区二区三区免费| 97久久国产综合精品女不卡 | 久久精品国产亚洲av影院| 久久免费99精品国产自在现线 | 欧美久久综合性欧美| 久久SE精品一区二区|