哈夫曼編碼C++實現__html5

來源:互聯網
上載者:User
#include <stdio.h>#include <limits.h>#include <string.h>#include <stdlib.h>#define N 6typedef struct huffNode{    unsigned int weight;   //權重    unsigned int lchild,rchild,parent;  //左右子節點和父節點}HTNode,*HuffTree;typedef char **HuffCode;//找出數組中無父節點且權值最小的兩個節點下標,分別用s1和s2儲存void select(const HuffTree &HT,int n,int &s1,int &s2);//HT:哈夫曼樹,HC:哈夫曼編碼,w:構造哈夫曼樹節點的權值,n:構造哈夫曼樹節點的個數void HuffmanCode(HuffTree &HT,HuffCode &HC,int *w,int n);int main(){    int i;    char key[N] = {'0','A','B','C','D','E'};//第0個元素保留不用    int w[N] = {0,1,2,4,5,6}; //第0個元素保留不用    HuffTree HT;    HuffCode HC;    HuffmanCode(HT,HC,w,N - 1);    for ( i = 1; i < N; i++ )    printf("%c:%s\n",key[i],HC[i]);    printf("\n");    return 0;}//找出數組中權值最小的兩個節點下標,分別用s1和s2儲存void select(const HuffTree &HT,int n,int &s1,int &s2){    int i;    s1 = s2 = 0;    int min1 = INT_MAX;//最小值,INT_MAX在<limits.h>中定義的    int min2 = INT_MAX;//次小值    for ( i = 1; i <= n; ++i )    {    if ( HT[i].parent == 0 )    {//篩選沒有父節點的最小和次小權值下標        if ( HT[i].weight < min1 )        {//如果比最小值小        min2 = min1;        s2 = s1;        min1 = HT[i].weight;        s1 = i;        }        else if ( (HT[i].weight >= min1) && (HT[i].weight < min2) )        {//如果大於等於最小值,且小於次小值        min2 = HT[i].weight;        s2 = i;        }        else        {//如果大於次小值,則什麼都不做        ;        }    }    }}//HT:哈夫曼樹,HC:哈夫曼編碼,w:構造哈夫曼樹節點的權值,n:構造哈夫曼樹節點的個數void HuffmanCode(HuffTree &HT,HuffCode &HC,int *w,int n){    int s1;    int s2;    int m = 2 * n - 1;       //容易知道n個節點構造的哈夫曼樹是2n-1個節點    int i,c,f,j;    char *code;  //暫存編碼的    HT = (HuffTree)malloc((m+1)*sizeof(HTNode));  //0單元未使用    for ( i = 1; i <= n; i++ )        HT[i] = {w[i],0,0,0};//初始化前n個節點(構造哈夫曼樹的原始節點)    for ( i = n + 1; i <= m; i++ )    HT[i] = {0,0,0,0};  //初始化後n-1個節點    //構建哈夫曼樹    for ( i = n + 1; i <= m; i++)    {    select(HT,i-1,s1,s2);//找出前i-1個節點中權值最小的節點下標    HT[s1].parent = i;    HT[s2].parent = i;    HT[i].lchild = s1;    HT[i].rchild = s2;    HT[i].weight = HT[s1].weight + HT[s2].weight;    }    //哈夫曼編碼    HC = (char **)malloc((n)*sizeof(char *));    //暫存編碼    code = (char *)malloc(n*sizeof(char));//使用了第0單元    code[n-1] = '\0';    for ( i = 1; i <= n; i++ )    {        int start = n-1;    for ( c = i, f = HT[c].parent; f != 0; c = HT[c].parent, f = HT[c].parent )    {//從葉子掃描到根        if ( HT[f].lchild == c )        {        code[--start] = '0';        }        else if(HT[f].rchild == c)        {        code[--start] = '1';        }        else        {//否則什麼也不做        ;        }    }    HC[i] = (char *)malloc(strlen(code)*sizeof(char));    strcpy(HC[i],&code[start]);    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.