哈夫曼编/译码器

Description

写一个哈夫曼码的编/译码系统,要求能对要传输的报文进行编码和解码。构造哈夫曼树时,权值小的放左子树,权值大的放右子树,编码时右子树编码为1,左子树编码为0。

Input

输入表示字符集大小为n(n <= 100)的正整数,以及n个字符和n个权值(正整数,值越大表示该字符出现的概率越大);输入串长小于或等于100的目标报文。

Output

经过编码后的二进制码,占一行;
以及对应解码后的报文,占一行;
最后输出一个回车符

  • Sample Input 
    5 a b c d e 12 40 15 8 25
    bbbaddeccbbb
  • Sample Output
    00011111110111010110110000
    bbbaddeccbbb
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct HTNode{
    int weight;
    int parent;
    int lchild;
    int rchild;
    char data;
}HNodeType;

typedef struct HCNode{
    int bit[200];
    int start;
}HCodeType;

HNodeType huffmanNode[1005];
HCodeType huffmanTree[200];
int str[1005]={0};
int p=0;
void HuffmanTree(HNodeType huffmanNode[], int n)
{
    int i,j;
    char x;
    for(i = 0; i < 2*n-1; i++)
    {
        huffmanNode[i].weight = 0;
        huffmanNode[i].parent = -1;
        huffmanNode[i].lchild = -1;
        huffmanNode[i].rchild = -1;
        huffmanNode[i].data = -1;
    }
    for(i = 0; i < n; i++)
    {
        scanf("%c",&x);
        scanf("%c",&huffmanNode[i].data);
    }
    scanf("%c",&x);
    for(i = 0; i < n; i++)
        scanf("%d",&huffmanNode[i].weight);
    for(i = 0; i < n-1; i++)
    {
        int m1, m2;
        m1 = m2 = 1000;
        int x1, x2;
        x1 = x2 = 0;
        for(j = 0; j < n+i; j++)
        {
            if((huffmanNode[j].weight<m1) && (huffmanNode[j].parent==-1))
            {
                m2 = m1;
                x2 = x1;
                m1 = huffmanNode[j].weight;
                x1 = j;
            }
            else if((huffmanNode[j].weight<m2) && (huffmanNode[j].parent==-1))
            {
                m2 = huffmanNode[j].weight;
                x2 = j;
            }
        }
        huffmanNode[x1].parent = n+i;
        huffmanNode[x2].parent = n+i;
        huffmanNode[n+i].weight = huffmanNode[x1].weight+huffmanNode[x2].weight;
        huffmanNode[n+i].lchild = x1;
        huffmanNode[n+i].rchild = x2;
    }
}

void decoding(int str[],HNodeType huffmanTree[],int n)
{
    int num = 2*n-1;
    int i =0;
    int temp;
    while(i < p)
    {
        temp = num-1;
        while((huffmanTree[temp].lchild!=-1)&&(huffmanTree[temp].rchild!=-1))
        {
            if(str[i] == 0)
                temp = huffmanTree[temp].lchild;
            else
                temp = huffmanTree[temp].rchild;
            i++;
        }
        printf("%c", huffmanTree[temp].data);
    }

}
void getNum(HCodeType huffmanTree[],int n)
{
    int i,j;
    HCodeType cd;
    for(i = 0; i < n; i ++)
    {
        cd.start = n-1;
        int cur = i;
        int p = huffmanNode[cur].parent;
        while(p!=-1)
        {
            if(huffmanNode[p].lchild == cur)
                cd.bit[cd.start] = 0;
            else
                cd.bit[cd.start] = 1;
            cd.start--;
            cur = p;
            p = huffmanNode[cur].parent;
        }
        for(j = cd.start+1; j<n; j++)
            huffmanTree[i].bit[j] = cd.bit[j];
        huffmanTree[i].start = cd.start;
    }
}
void print(int n)
{
    char code[1000];
    int i,j,k;
    scanf("%s", code);
    for(i = 0; i < (int)strlen(code); i++)
    {
      for(j = 0; j < n; j++)
        {
            if(code[i] == huffmanNode[j].data)
            {
                for(k = huffmanTree[j].start+1; k < n; k++)
                {
                    printf("%d", huffmanTree[j].bit[k]);
                    str[p]=huffmanTree[j].bit[k];
                    p++;
                }
            }
        }
    }
    printf("\n");
}
int main()
{
    int n,i,j;
    scanf("%d",&n);
    HuffmanTree(huffmanNode,n);
    getNum(huffmanTree,n);
    print(n);
    decoding(str,huffmanNode,n);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zhao2018/article/details/80196083