huffman coding (1)

topic description

Given the weights of n characters (the weights are all positive integers greater than 0), construct a Huffman tree HT, and find the Huffman code HC of these n characters.
Note: When constructing the Huffman tree HT, when merging two binary trees into a new binary tree, use the root node with a small weight as the left subtree! 

enter

First enter the number n of weights (n>1).
Then input n weights in turn (the weights are all positive integers greater than 0)

output

Corresponding to the input n weights, the corresponding codes are sequentially output.
When encoding, the left-child branch is coded as 0 and the right-child branch is coded as 1.

#include<iostream>
#include<cstring>
#include<string.h>
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)
{
   /****在此下面完成代码***************/
    通过flag和parent来实现求最小值s1和次小值s2,避免使用中间变量最小值min
    int i,flag=0;
    for(i=1;i<=len;i++)
    {
        if(flag==0&&HT[i].parent==0)
        {
            s1=i;flag=1;
        }
        if(flag==1&&HT[i].weight<HT[s1].weight&&HT[i].parent==0)
        {
            s1=i;
        }
     } 
     for(i=1;i<=len;i++)
    {
        //这里i不能和s1相等
        if(flag==1&&HT[i].parent==0&&i!=s1)
        {
            s2=i;flag=0;
        }
        if(flag==0&&HT[i].weight<HT[s2].weight&&HT[i].parent==0&&i!=s1)
        {
            s2=i;
        }
     } 
    
   /***********************************/
}
    
//用算法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<=n;++i)           //输入前n个单元中叶子结点的权值
        cin>>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)
{
    //char cd[];
    int i,start,c,f;
    HC=new char*[n+1];
    char *cd=new char[n];
    cd[n-1]='\0';
    for(i=1;i<=n;i++)
    {
        start=n-1;
        c=i;
        f=HT[i].parent;
        while(f)
        {
            --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;
}
int main()
{
    HuffmanTree HT;
    HuffmanCode HC;
    int n;
    cin>>n;                                          //输入赫夫曼树的叶子结点个数
    CreatHuffmanTree(HT,n);
    CreatHuffmanCode(HT,HC,n);
    for(int i=1;i<=n;i++)
    {
        cout<<HC[i]<<endl;
    }
    return 0;
}

Sample input Copy

8
5 29 7 8 14 23 3 11

Sample output Copy

0001
10
1110
1111
110
01
0000
001

Guess you like

Origin blog.csdn.net/qq_63306482/article/details/124567081