哈夫曼树编解码

问题 B: DS_6.14 给定报文,哈弗曼编码、译码(by Yan)
时间限制: 20 Sec 内存限制: 256 MB
提交: 303 解决: 218
[提交][状态][讨论版]
题目描述
已知某段通信报文内容,对该报文进行哈弗曼编码,并计算平均码长。
(1)统计报文中各字符出现的频度。(字符集范围为52个英文字母,空格,英文句号。报文长度<=200)
(2)构造一棵哈弗曼树,依次给出各字符编码结果。
(3)给字符串进行编码。
(4)给编码串进行译码。
(5)计算平均码长。
规定:
(1)结点统计:以ASCII码的顺序依次排列,例如:空格,英文句号,大写字母,小写字母。
(2)构建哈弗曼树时:左子树根结点权值小于等于右子树根结点权值。
(3)选择的根节点权值相同时,前者构建为双亲的左孩子,后者为右孩子。
(4)生成编码时:左分支标0,右分支标1。
输入
第一部分:报文内容,以’#'结束。
第二部分:待译码的编码串。
输出
依次输出报文编码串、译码串、平均码长,三者之间均以换行符间隔。
平均码长,保留小数点2位。
样例输入

Data structure is the way of computer storage and organization data. A data structure is a collection of data elements that exist between one or more specific relationships.#
000111111110101111110100101010000110111100010011011000001110011110011100101011010011100001101111011001011010101011001101110010101011101011110110101000110001101001010101010001111000101001110111111101000001101010100000010111111110101110110011111101001111010111011100000011110101111000100110000111011000000111101011111101001010100001101111000100110110000011100111100111011111101110010100000100001001111000100111101011101110101010110011000000111101011111100010000100110111000111101010100111001010110111110101100010110001011110010101100110000001010000110001001111011101010111010011101010100011010111010101000001110100110111100111100011110110001111110011010000010000111110100111101011101100110110101111011111001000100

样例输出

000111111110101111110100101010000110111100010011011000001110011110011100101011010011100001101111011001011010101011001101110010101011101011110110101000110001101001010101010001111000101001110111111101000001101010100000010111111110101110110011111101001111010111011100000011110101111000100110000111011000000111101011111101001010100001101111000100110110000011100111100111011111101110010100000100001001111000100111101011101110101010110011000000111101011111100010000100110111000111101010100111001010110111110101100010110001011110010101100110000001010000110001001111011101010111010011101010100011010111010101000001110100110111100111100011110110001111110011010000010000111110100111101011101100110110101111011111001000100
Data structure is the way of computer storage and organization data. A data structure is a collection of data elements that exist between one or more specific relationships.
4.11

#include
#include
#include
#define LettersNum 6
#define TextCharTypeNum 54
typedef struct{//三叉链表的结点结构体定义
	int weight;
	int Parent;
	int Lchild;
	int Rchild;
}HFTreeNode;
void SelectTwoMinimumWeightNodes(HFTreeNode Tree[],int Range,int *Node1,int *Node2)//在哈夫曼树数组从第一个元素到第Range元素找出两个不重复的权值最小的树结点
{
    int cmp = 998,i;
    for(i=1;i<=Range;i++)
    {
        if(Tree[i].weight=0;j--)
    {
        if(Tree[Tree[i].Parent].Lchild==i)
        {
            (*Aim)[j] = '0';
            i = Tree[i].Parent;

        }
        else if(Tree[Tree[i].Parent].Rchild==i)
        {
            (*Aim)[j] = '1';
            i = Tree[i].Parent;
        }

    }
    (*Aim)[Codelength]='\0';
    return;
}
void PrintLetterCorrespondingCodes(char *Codes[],int CodesSize,char ValidLetters[])
{
    int i;
    for(i=0;imaxCodeLength)maxCodeLength = strlen(Codes[i]);
    //printf("最长码长度%d\n",maxCodeLength);
    AimLen = strlen(Aim);
    //printf("二进制串长度%d\n",AimLen);
    i=0;
    while(imaxcmp)
            {
                maxcmp = j;
                flag = k;

            }
        }
        printf("%c",ValidLetters[flag]);
        i+=strlen(Codes[flag]);
        //printf("此段译码长度%d\n",strlen(Codes[flag]));
    }
    return;
}
void CountTextCharNums(int *TextCharWeights,char Text[])
{
    int i;
    for(i=0;i='A'&&Text[i]<='Z')TextCharWeights[Text[i]-'A']++;
        else if(Text[i]>='a'&&Text[i]<='z')TextCharWeights[Text[i]-'a'+(TextCharTypeNum-2)/2]++;

    }
    return;
}
float ComputeAverageBinStrLength(char *Codes[],int CodesSize)
{
    unsigned i;
    float result,Sum=0;
    for(i=0;i0)Num++;
    }
    return Num;
}
void CopytoNewWeightsArr(int *NewWeights,int OldWeights[],char *ValidLetters,int ValidWeights,char TextChar[])
{
    int i,k=0;
    for(i=0;i0&&k

你可能感兴趣的:(c语言)