简易输出Huffman编码

来源:互联网 发布:ps4直播 mac 编辑:程序博客网 时间:2024/06/05 11:24
#include<iostream>#include<string>using namespace std;struct hufftree{    int parent;    int lchild;    int rchild;    int weight;    string flag;};struct lowestnode{    char ch;                 //统计各个字符    int ch_num;              //字符的频率};void coding(int length,hufftree tree[],int n,int &a,int &b){                            //函数功能:一组数中选择出最小的两个数    int i;                       int r,s;                     r=s=length;    for(i=0;i<n;i++)    {        if((tree[i].weight<r)&&(tree[i].parent==-1))        {                    //"<"而不是"<="代表若最小的数有多个选择前            r=tree[i].weight;  //面的旧的两个这样避免增加树的层数            a=i;        }    }    for(i=0;i<n;i++)    {        if((tree[i].weight<s)&&(i!=a)&&(tree[i].parent==-1))        {            s=tree[i].weight;            b=i;        }    }}void free(string str){    int length=str.length();    lowestnode *node=new lowestnode[length];    int i,j;    for(i=0;i<length;i++)    {        node[i].ch_num=0;    }    int type=0;    for(i=0;i<length;i++)    {        for(j=0;j<type;j++)        {            if(str[i]==node[j].ch||(node[j].ch>='a'&&node[j].ch<='z'&&str[i]+32==node[j].ch))            {                node[j].ch_num++;                break;            }        }        if(j==type)        {            if(str[i]>='A'&&str[i]<='Z')                node[j].ch=str[i]+32;            else                node[j].ch=str[i];            node[j].ch_num++;            type++;        }    }                          //统计字符及频率    for(i=0;i<type;i++)        cout<<"字符"<<node[i].ch<<"出现了"<<node[i].ch_num<<"次"<<endl;    hufftree *huff=new hufftree[2*type-1];    hufftree temp;    string *code=new string[2*type-1];    for(i=0;i<2*type-1;i++)    {                                   //结点初始化        huff[i].lchild=-1;        huff[i].rchild=-1;        huff[i].parent=-1;        huff[i].flag=-1;    }    for(j=0;j<type;j++)    {        huff[j].weight=node[j].ch_num;   //赋权重    }    int min1,min2;    for(int k=type;k<2*type-1;k++)    {        coding(length,huff,k,min1,min2); //codeing函数选出的数当成        huff[min1].parent=k;             //孩子结点        huff[min2].parent=k;        huff[min1].flag="0";        huff[min2].flag="1";        huff[k].lchild=min1;        huff[k].rchild=min2;        huff[k].weight=huff[min1].weight+huff[min2].weight;    }    for(i=0;i<type;i++)    {        temp=huff[i];        while(1)        {            code[i]=temp.flag+code[i];   //string类重载+号:倒叙加            temp=huff[temp.parent];      //起来            if(temp.parent==-1)                break;        }    }    cout<<"字符串的每个字符huffman编码为:"<<endl;    for(i=0;i<type;i++)        cout<<node[i].ch<<" "<<code[i]<<endl;    cout<<"整个字符串的huffman编码为:"<<endl;    for(i=0;i<length;i++)    {        for(j=0;j<type;j++)        {            if(str[i]==node[j].ch)                cout<<code[j];        }    }    delete[] node;    node=NULL;    delete[] huff;    huff=NULL;    delete[] code;    code=NULL;}void main(){    int length=0;    string str;    cout<<"请输入一个字符串:";    cin>>str;    free(str);}

这是《c++程序设计经典**》的15题,优化了一下。

0 0