哈夫曼树以及哈夫曼编码的实现

typedef struct
{
    unsigned int weight;
    unsigned int parent,lchild,rchild;
}HTNode,*HuffmanTree;

typedef char **HuffmanCode;

void HuffmanCoding(HuffmanTree &ht,HuffmanCode &hc,int *w,int n);
void Select(HuffmanTree ht,int n,int &s1,int &s2);

void HuffmanCoding(HuffmanTree &ht,HuffmanCode &hc,int *w,int n)
{
    //w存放的是每个字符的权值,w的0号单位未用
    //构造哈夫曼树ht,并求出n个字符的哈夫曼树编码hc

    //初始化
    if(n<=1)
        return;
    int m=2*n-1;                                   //一棵有n个叶子结点的哈夫曼树共有2n-1个结点
    ht=(HuffmanTree)malloc((m+1)*sizeof(HTNode));  //0号单元未用
    HuffmanTree p;
    int i;
    for(p=ht+1,i=1;i<=n;++i,++p)                   //每棵树只有带权值为w[i]的根,左右子树为空
    {
        p->weight=w[i];
        p->parent=p->lchild=p->rchild=0;
    }
    for(;i<=m;i++,p++)
        p->parent=p->lchild=p->rchild=p->weight=0;
    int s1,s2;

    //建立哈夫曼树
    for(i=n+1;i<=m;i++)
    {
        //Select函数在已有的ht[1..i-1]中选择parent为0且weight最小的两个结点,赋值给s1、s2
        Select(ht,i-1,s1,s2);
        ht[s1].parent=ht[s2].parent=i;
        ht[i].lchild=s1;
        ht[i].rchild=s2;
        ht[i].weight=ht[s1].weight+ht[s2].weight;           //选取权值最小的树作为左右子树构造一棵新的二叉树
    }


    //从叶子到根逆向求每个字符的哈夫编码
    hc=(HuffmanCode)malloc((n+1)*sizeof(char *));
    char *cd=(char *)malloc(n*sizeof(char));
    cd[n-1]='\0';
    for(i=1;i<=n;i++)                                       //逐个字符求哈夫曼编码
    {
        int start=n-1;
        int c,f;
        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';
        }
        hc[i]=(char *)malloc((n-start)*sizeof(char));       //为第i个字符编码分配空间
        strcpy(hc[i],&cd[start]);                           //从cd复制到hc[i]中
    }
}

void Select(HuffmanTree ht,int n,int &s1,int &s2)
{
    int flag[n+1];
    for(int i=0;i<n+1;i++)
        flag[i]=0;
    int temp,i,j;

    //找出第一个parent为0且值最小的结点
    for(i=1;i<=n;i++)
    {
        if(ht[i].parent==0)
        {
            temp=ht[i].weight;
            j=i;
        }
    }
    for(i=1;i<=n;i++)
    {
        if(temp>ht[i].weight&&ht[i].parent==0)
        {
            temp=ht[i].weight;
            j=i;
        }
    }
    s1=j;flag[j]=1;

    //找出第二个parent为0且值最小的结点,不同于第一个
    for(i=1;i<=n;i++)
    {
        if(ht[i].parent==0&&flag[i]==0)
        {
            temp=ht[i].weight;
            j=i;
        }
    }
    for(i=1;i<=n;i++)
    {
        if(temp>ht[i].weight&&flag[i]==0&&ht[i].parent==0)
        {
            temp=ht[i].weight;
            j=i;
        }
    }
    s2=j;flag[j]=1;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
//算法5.11 根据赫夫曼树求赫夫曼编码 #include using namespace std; typedef struct { int weight; int parent,lchild,rchild; }HTNode,*HuffmanTree; typedef char **HuffmanCode; void Select(HuffmanTree HT,int len,int &s1,int &s2) { int i,min1=0x3f3f3f3f,min2=0x3f3f3f3f;//先赋予最大值 for(i=1;i<=len;i++) { if(HT[i].weight<min1&&HT[i].parent==0) { min1=HT[i].weight; s1=i; } } int temp=HT[s1].weight;//将原值存放起来,然后先赋予最大值,防止s1被重复选择 HT[s1].weight=0x3f3f3f3f; for(i=1;i<=len;i++) { if(HT[i].weight<min2&&HT[i].parent==0) { min2=HT[i].weight; s2=i; } } HT[s1].weight=temp;//恢复原来的值 } //用算法5.10构造赫夫曼树 void CreatHuffmanTree(HuffmanTree &HT,int n) { //构造赫夫曼树HT int m,s1,s2,i; if(n<=1) return; m=2*n-1; HT=new HTNode[m+1]; //0号单元未用,所以需要动态分配m+1个单元,HT[m]表示根结点 for(i=1;i<=m;++i) //将1~m号单元中的双亲、左孩子,右孩子的下标都初始化为0 { HT[i].parent=0; HT[i].lchild=0; HT[i].rchild=0; } cout<<"请输入叶子结点的权值:\n"; for(i=1;i>HT[i].weight; /*――――――――――初始化工作结束,下面开始创建赫夫曼树――――――――――*/ for(i=n+1;i<=m;++i) { //通过n-1次的选择、删除、合并来创建赫夫曼树 Select(HT,i-1,s1,s2); //在HT[k](1≤k≤i-1)中选择两个其双亲域为0且权值最小的结点, // 并返回它们在HT中的序号s1和s2 HT[s1].parent=i; HT[s2].parent=i; //得到新结点i,从森林中删除s1,s2,将s1和s2的双亲域由0改为i HT[i].lchild=s1; HT[i].rchild=s2 ; //s1,s2分别作为i的左右孩子 HT[i].weight=HT[s1].weight+HT[s2].weight; //i 的权值为左右孩子权值之和 } //for } // CreatHuffmanTree void CreatHuffmanCode(HuffmanTree HT,HuffmanCode &HC,int n) { //从叶子到根逆向求每个字符的赫夫曼编码,存储在编码表HC中 int i,start,c,f; HC=new char*[n+1]; //分配n个字符编码的头指针矢量 char *cd=new char[n]; //分配临时存放编码的动态数组空间 cd[n-1]='\0'; //编码结束符 for(i=1;i<=n;++i) { //逐个字符求赫夫曼编码 start=n-1; //start开始时指向最后,即编码结束符位置 c=i; f=HT[i].parent; //f指向结点c的双亲结点 while(f!=0) { //从叶子结点开始向上回溯,直到根结点 --start; //回
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值