第十一週項目1(4)哈夫曼編碼的算法驗證

問題及代碼:

#ifndef BTREE_H_INCLUDED  
#define BTREE_H_INCLUDED  
/*    
*煙臺大學計控學院     
*作    者:孫啓先   
*完成日期:2016年11月10日 
*問題描述:根據哈夫編碼樹求對應的哈夫編碼的算法 
*/  
#endif // BTREE_H_INCLUDED  

#include <stdio.h>  
#include <string.h>  
  
#define N 50        //葉子結點數  
#define M 2*N-1     //樹中結點總數  
  
//哈夫曼樹的節點結構類型  
typedef struct  
{  
    char data;  //結點值  
    double weight;  //權重  
    int parent;     //雙親結點  
    int lchild;     //左孩子結點  
    int rchild;     //右孩子結點  
} HTNode;  
  
//每個節點哈夫曼編碼的結構類型  
typedef struct  
{  
    char cd[N]; //存放哈夫曼碼  
    int start;  
} HCode;  
  
//構造哈夫曼樹  
void CreateHT(HTNode ht[],int n)  
{  
    int i,k,lnode,rnode;  
    double min1,min2;  
    for (i=0; i<2*n-1; i++)         //所有結點的相關域置初值-1  
        ht[i].parent=ht[i].lchild=ht[i].rchild=-1;  
    for (i=n; i<2*n-1; i++)         //構造哈夫曼樹  
    {  
        min1=min2=32767;            //lnode和rnode爲最小權重的兩個結點位置  
        lnode=rnode=-1;  
        for (k=0; k<=i-1; k++)  
            if (ht[k].parent==-1)   //只在尚未構造二叉樹的結點中查找  
            {  
                if (ht[k].weight<min1)  
                {  
                    min2=min1;  
                    rnode=lnode;  
                    min1=ht[k].weight;  
                    lnode=k;  
                }  
                else if (ht[k].weight<min2)  
                {  
                    min2=ht[k].weight;  
                    rnode=k;  
                }  
            }  
        ht[i].weight=ht[lnode].weight+ht[rnode].weight;  
        ht[i].lchild=lnode;  
        ht[i].rchild=rnode;  
        ht[lnode].parent=i;  
        ht[rnode].parent=i;  
    }  
}  
  
//實現哈夫曼編碼  
void CreateHCode(HTNode ht[],HCode hcd[],int n)  
{  
    int i,f,c;  
    HCode hc;  
    for (i=0; i<n; i++) //根據哈夫曼樹求哈夫曼編碼  
    {  
        hc.start=n;  
        c=i;  
        f=ht[i].parent;  
        while (f!=-1)   //循序直到樹根結點  
        {  
            if (ht[f].lchild==c)    //處理左孩子結點  
                hc.cd[hc.start--]='0';  
            else                    //處理右孩子結點  
                hc.cd[hc.start--]='1';  
            c=f;  
            f=ht[f].parent;  
        }  
        hc.start++;     //start指向哈夫曼編碼最開始字符  
        hcd[i]=hc;  
    }  
}  
  
//輸出哈夫曼編碼  
void DispHCode(HTNode ht[],HCode hcd[],int n)  
{  
    int i,k;  
    double sum=0,m=0;  
    int j;  
    printf("  輸出哈夫曼編碼:\n"); //輸出哈夫曼編碼  
    for (i=0; i<n; i++)  
    {  
        j=0;  
        printf("      %c:\t",ht[i].data);  
        for (k=hcd[i].start; k<=n; k++)  
        {  
            printf("%c",hcd[i].cd[k]);  
            j++;  
        }  
        m+=ht[i].weight;  
        sum+=ht[i].weight*j;  
        printf("\n");  
    }  
    printf("\n  平均長度=%g\n",1.0*sum/m);  
}  
  
int main()  
{  
    int n=8,i;      //n表示初始字符串的個數  
    char str[]= {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};  
    double fnum[]= {0.07,0.19,0.02,0.06,0.32,0.03,0.21,0.1};  
    HTNode ht[M];  
    HCode hcd[N];  
    for (i=0; i<n; i++)  
    {  
        ht[i].data=str[i];  
        ht[i].weight=fnum[i];  
    }  
    printf("\n");  
    CreateHT(ht,n);  
    CreateHCode(ht,hcd,n);  
    DispHCode(ht,hcd,n);  
    printf("\n");  
    return 0;  
}  


運行結果:


知識點總結:

哈夫曼編碼的實質是使用頻率越高的字符采用越短的編碼。就是先找連個最小的節點相加然後在找小的值與雙親節點相加直至找到根節點

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章