<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>

新聞中心

EEPW首頁(yè) > 嵌入式系統 > 設計應用 > 二叉堆的C語(yǔ)言實(shí)現

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

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

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

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

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

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

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

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

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

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

/*將最后需要釋放內存空間的值保存到第一個(gè)內存空間中*/
heap->parray[1] = heap->parray[heap->currentSize --];
/*從表頭開(kāi)始將新的二叉樹(shù)進(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;
}

測試結果:

[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


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


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

關(guān)鍵詞: 二叉堆C語(yǔ)言數據結

評論


技術(shù)專(zhuān)區

關(guān)閉
国产精品自在自线亚洲|国产精品无圣光一区二区|国产日产欧洲无码视频|久久久一本精品99久久K精品66|欧美人与动牲交片免费播放
<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>