哈弗曼树

#include"stdio.h"
#include"string.h"
#include"malloc.h"
#include"iostream"
using namespace std;
typedef struct{
    unsigned int weight;
    unsigned int parent,lchild,rchild;
}HTNode,*HuffTree;
typedef  char  **HuffCode;


void Select(HuffTree &Ht, int m, int &S1, int &S2)     
{

    int j;
    S1 = 0;
    S2 = 0;
    for (j = 1; j <= m; j++)
    {
        if (Ht[j].parent == 0 && Ht[j].weight != 0)
        {
            if (Ht[j].weight<Ht[S1].weight)
                S1 = j;
        }
    }
    for (j = 1; j <= m; j++)
    {
        if (Ht[j].parent == 0 && j != S1&&Ht[j].weight != 0)
        {
            if (Ht[j].weight<Ht[S2].weight)
                S2 = j;
        }
    }

} 



void HuffmanCoding(HuffTree &Ht,HuffCode &Hc,int n){  //哈夫曼编码函数 
if(n<=1)return;
int m,i,s1, s2;
HuffTree p;
m =2*n-1; 
Ht = (HuffTree)malloc((m + 1) * sizeof(HTNode));
string ch;
for (p = Ht + 1, i = 1; i <= m; ++i, ++p) {     //初始化哈夫曼树各个值 
    p->weight = 0;
    p->parent = 0;
    p->lchild = 0;
    p->rchild = 0;
}
cin >> ch;                                   //读入一段文本 
for (int i = 0; ch[i] !='\0'; i++) {
    Ht[ch[i] - 'a'+1].weight++;               //当文本还没有结束时,统计各个字符的权值 
}
for(i=n+1;i<=m;++i){                         //建哈夫曼树 
    Select(Ht,i-1,s1,s2);                      
    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=(HuffCode)malloc((n+1)*sizeof(char*));
char * cd=(char*)malloc((n)*sizeof(char));
int c,f;
cd[n-1]='\0';                 //编码结束符 
int start;
for(i=1;i<=n;++i){
    start=n-1;
    for(c=i,f=Ht[i].parent;f!=0;c=f,f=Ht[f].parent)
    {if(Ht[f].lchild==c)cd[--start]='0';
    else cd[--start]='1';
    }
    //cd[n-1]='\0';
    Hc[i]=(char *)malloc((n-start)*sizeof(char));
    strcpy(Hc[i],&cd[start]);
}
free(cd);

}

void show(HuffCode &Hc, int n)//输出哈夫曼编码  
{
    int i, k;
    cout<<"  输出哈夫曼编码:\n"; //输出哈夫曼编码  
    for (i = 1; i<=n; i++)
    {
            cout<< Hc[i];
            //cout<<"\n"; 
    }

}
int main()
{
    HuffTree ht;
    HuffCode hc;
    int n;
    cout << "请输入文本";
    HuffmanCoding(ht,hc,26);
    show(hc,26);
    system("pause");
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值