資源簡介
哈工程本科算法實驗哈夫曼編碼【數據+代碼+說明+流程圖+測試用例】

代碼片段和文件信息
#include?
#define?MAXBIT?10?/*定義哈夫曼編碼的最大長度*/
#define?MAXVALUE?10000?/*定義最大權值*/
#define?MAXLEAF?30?/*定義哈夫曼樹中最多葉子節點個數*/
#define?MAXNODE?MAXLEAF*2-1?/*哈夫曼樹最多結點數*/
typedef?struct???/*哈夫曼編碼信息的結構*/
{
????int?bit[MAXBIT];
????int?start;
}?Hcodetype;
typedef?struct???/*哈夫曼樹結點的結構*/
{
????int?weight;
????int?parent;
????int?lchild;
????int?rchild;
}?Hnodetype;
void?huffmantree(Hnodetype?huffnode[MAXNODE]int?n)?/*構造哈夫曼樹的函數*/
{
????int?ijm1m2x1x2;
????for(i=0;?i<2*n-1;?i++)?/*存放哈夫曼樹結點的數組huffnode[]初始化*/
????{
????????huffnode[i].weight=0;
????????huffnode[i].parent=-1;
????????huffnode[i].lchild=-1;
????????huffnode[i].rchild=-1;
????}
????for(i=0;?i ????{
????????printf(“輸入第%d個節點的權值(頻率)\n“i+1);
????????scanf(“%d“&huffnode[i].weight);
????}
????for(i=0;?i ????{
????????m1=m2=MAXVALUE;
????????x1=x2=0;
????????for(j=0;?j ????????{
????????????if(huffnode[j].weight ????????????{
????????????????m2=m1;
????????????????x2=x1;
????????????????m1=huffnode[j].weight;
????????????????x1=j;
????????????}
????????????else?if(huffnode[j].weight ????????????{
????????????????m2=huffnode[j].weight;
????????????????x2=j;
????????????}
????????}
????????huffnode[x1].parent=n+i;
????????huffnode[x2].parent=n+i;
????????huffnode[n+i].weight=huffnode[x1].weight+huffnode[x2].weight;
????????huffnode[n+i].lchild=x1;
????????huffnode[n+i].rchild=x2;
????}
}
void?main()
{
????Hnodetype?huffnode[MAXNODE];
????Hcodetype?huffcode[MAXLEAF]cd;
????int?ijcpn;
????printf(“輸入要構造哈夫曼樹的節點個數?n\n“);
????scanf(“%d“&n);?/*輸入葉子節點個數*/
????while(n<1)
????{
????????printf(“error:輸入違法,無法構造哈夫曼樹?\n請重新輸入:\n“);
????????scanf(“%d“&n);?/*輸入葉子節點個數*/
????}
????huffmantree(huffnoden);?/*建立哈夫曼樹*/
????for(i=0;?i ????{
????????cd.start=n-1;
????????c=i;
????????p=huffnode[c].parent;
????????while(p!=-1)
????????{
????????????if(huffnode[p].lchild==c)?cd.bit[cd.start]=0;
????????????else?cd.bit[cd.start]=1;
????????????cd.start--;
????????????c=p;
????????????p=huffnode[c].parent;
????????}
????????for(j=cd.start+1;?j ????????????huffcode[i].bit[j]=cd.bit[j];
????????huffcode[i].start=cd.start;
????}
????for(i=0;?i ????{
????????printf(“第%d個節點的哈夫曼編碼:“i+1);
????????for(j=huffcode[i].start+1;?j ????????????printf(“%d“huffcode[i].bit[j]);
????????printf(“\n“);
????}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????2827??2016-12-06?09:04??哈夫曼編碼\哈夫曼.c
?????文件???????28844??2016-12-06?09:14??哈夫曼編碼\哈夫曼.exe
?????文件????????2053??2016-12-06?09:14??哈夫曼編碼\哈夫曼.o
?????文件???????10205??2016-12-06?09:17??哈夫曼編碼\測試用例_.xlsx
?????文件???????74805??2016-12-06?09:49??哈夫曼編碼\說明+流程圖.docx
?????目錄???????????0??2018-11-26?19:26??哈夫曼編碼\
評論
共有 條評論