6-1 哈夫曼树及哈夫曼编码

6-1 哈夫曼树及哈夫曼编码

分数 10

全屏浏览题目

切换布局

作者 wgy

单位 浙江大学

函数SelectTwoMin(int upbound, HuffmanTree HT, int &s1, int &s2)是从1到upbound中找出father为0的节点赋给s1,s2,(为了保证答案唯一,请让s1的节点编号小于s2),函数HuffmanCoding(HuffmanTree &HT, HuffmanCode &HC, int *w, int n)是构造哈夫曼树以及计算哈夫曼编码。保证输入的权重值小于1000。

函数接口定义:

 

void SelectTwoMin(int upbound, HuffmanTree HT, int &s1, int &s2); void HuffmanCoding(HuffmanTree &HT, HuffmanCode &HC, int *w, int n);

其中 upbound 编号,HT是哈夫曼树,HC是哈夫曼编码,w是权值,n是叶子节点个数。

裁判测试程序样例:

 

#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct { int weight; int parent; int lchild; int rchild; } HTNode, *HuffmanTree; typedef char ** HuffmanCode; void SelectTwoMin(int upbound, HuffmanTree HT, int &s1, int &s2); void HuffmanCoding(HuffmanTree &HT, HuffmanCode &HC, int *w, int n); int main() { HuffmanTree ht; HuffmanCode hc; int n; scanf("%d", &n); int *w = (int *) malloc (n * sizeof(int)); for(int i = 0; i < n; ++ i) scanf("%d", &w[i]); HuffmanCoding(ht, hc, w, n); for (int i = 1; i <= 2 * n - 1; ++ i) { printf("%d %d %d %d\n", ht[i].weight, ht[i].parent, ht[i].lchild, ht[i].rchild); } for (int i = 1; i <= n; ++ i) printf("%s\n", hc[i]); free(w); free(ht); for (int i = 1; i <= n; ++ i) free(hc[i]); return 0; } /* 你的代码将被嵌在这里 */

####输入格式:
第一行输入一个数n,表示叶子节点的个数,接下去输入n个整数,表示每个节点的值

####输出格式:
只要建树即可,输出已经确定了

输入样例:

4
1 2 3 4

输出样例:

1 5 0 0
2 5 0 0
3 6 0 0
4 7 0 0
3 6 1 2
6 7 3 5
10 0 4 6
110
111
10
0

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

关于这题的题解

void SelectTwoMin(int upbound, HuffmanTree HT, int &s1, int &s2)
{
    int m1=1000,m2=1000;
    int x1=0,x2=0;
    for(int i=1;i<=upbound;i++)
    {
        
        if(HT[i].parent==0&&HT[i].weight<m1)
        {
            
            m2=m1;
            x2=x1;
            m1=HT[i].weight;
            x1=i;
        }else if(HT[i].parent==0&&HT[i].weight<m2)
                {
                     m2=HT[i].weight;
                     x2=i;
               
                }
    }
    s1=x1;
    s2=x2;
}
void HuffmanCoding(HuffmanTree &HT, HuffmanCode &HC, int *w, int n)
{
    int s1=0,s2=0;
    int m=2*n-1;
    HC=new char*[n+1];
   //HT = (HuffmanTree)malloc(sizeof(HTNode)*(2*n));
   // HC = (char **)malloc(sizeof(char *)*(n+1));
    HT=new HTNode[m+1];
    /* for(int i=1;i<=n;i++)
    {
    HC[i] = (char *)malloc(sizeof(char)*(n+1));
memset(HC[i],0,sizeof(char)*(n+1));
}  */
    for(int i=0;i<n;i++)
    {
        
        HT[i+1].weight=w[i];
    }
    for(int i=1;i<=m;i++)
    {
        
        HT[i].lchild=0;
        HT[i].rchild=0;
        HT[i].parent=0;
    }

    for(int i=n+1;i<=m;i++)
    {
        SelectTwoMin(i-1,HT,s1,s2);
        
        HT[i].lchild=s1;
        HT[i].rchild=s2;
        HT[s1].parent=i;
        HT[s2].parent=i;
        HT[i].weight=HT[s1].weight+HT[s2].weight;
    }
   
   
    for(int i=1;i<=n;i++)
    {
         char cd[n];
         cd[n-1]='\0';
        int start=n-1;
        int c=i;
        int f=HT[i].parent;
        while(f!=0)
        {
            start--;
            if(HT[f].lchild==c) {cd[start]='0';}
            else {cd[start]='1';}
            c=f; f=HT[f].parent;
            
        }
        HC[i]=new char[n-start];
        strcpy(HC[i],&cd[start]);
    }
    //delete cd;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值