给定报文中26个字母a-z及空格的出现频率{64, 13, 22, 32, 103, 21, 15, 47, 57, 1, 5, 32, 20, 57, 63, 15, 1, 48,51, 80, 23, 8, 18, 1, 16, 1, 168},构建赫夫曼树并为这27个字符编制赫夫曼编码,并输出。模拟发送端,从键盘输入字符串,以%为结束标记,在屏幕上输出输入串的编码;模拟接收端,从键盘上输入0-1赫夫曼编码串,翻译出对应的原文。
l 模拟发送端
输入:I love you
输出:01101111011110011100000010111100011100100001
l 模拟接收端输入
输入:01101101111011000111111010111101101001100001
输出:it is a dog
#include <iostream> #include <algorithm> #include <queue> #include <stdlib.h> using namespace std; const int N=100; typedef struct tree{ char data; int weight; struct tree *lchild,*rchild; }tree; tree *tre[28]; bool cmp(tree a,tree b){ return a.weight>b.weight; } int weight[27],path[N],code[28][28]; int a1(){ for(int i=0;i<27;i++) cin>>weight[i]; return 0; } int a2(){ tree *p; for(int i=0;i<27;i++) { tre[i]=(tree*)malloc(sizeof(tree)); tre[i]->data='a'+i; tre[i]->weight=weight[i]; tre[i]->lchild=tre[i]->rchild=NULL; } p=(tree*)malloc(sizeof(tree)); for(int i=0;i<27;i++) for(int j=i+1;j<27;j++) { if(tre[i]->weight<tre[j]->weight) { p=tre[i]; tre[i]=tre[j]; tre[j]=p; } } int text=27; while(--text>0) { p=(tree*)malloc(sizeof(tree)); p->lchild=tre[text]; p->rchild=tre[text-1]; p->weight=tre[text]->weight+tre[text-1]->weight; for(int i=text-2;i>=0;i--) { if(tre[i]->weight > p->weight) { tre[i+1]=p; break; } tre[i+1]=tre[i]; if( i==0 ) { tre[i]=p; } } } tre[0]=p; return 0; } int a3(tree *t,int i,int j){ path[j]=i; int k; if(t->lchild) a3(t->lchild,0,j+1); if(t->rchild) a3(t->rchild,1,j+1); if( (!t->lchild&&!t->rchild)) { for( k=0;k<=j;k++) code[t->data-'a'][k]=path[k]; code[t->data-'a'][27]=k; } } int a4(){ for(int i=0;i<27;i++) { if(i!=26) { cout<<"["<<char('a'+i)<<"]: "; for(int j=1;j<code[i][27];j++) cout<<code[i][j]; cout<<endl; } else { cout<<"[ ]: "; for(int j=1;j<code[i][27];j++) cout<<code[i][j]; cout<<endl; } } } int a5(){ queue<int>q; int m=1; char c; scanf("%c",&c); while(m) { scanf("%c",&c); switch(c) { case '\n': m=0;break; case ' ': q.push(26);break; default : q.push(c-'a');break; } } while(!q.empty()) { int i=q.front(); q.pop(); for(int j=1;j<code[i][27];j++) cout<<code[i][j]; } cout<<endl; } int a6(tree *l){ tree *t;t=l; queue<int>q; int m=1; char c;scanf("%c",&c); while(m) { scanf("%c",&c); switch(c) { case '\n': m=0;break; default : q.push(c-'0');break; } } while(!q.empty()) { int i=q.front(); q.pop(); if(i==0) { t=t->lchild; if((!t->lchild&&!t->rchild)) { if(t->data=='{') cout<<" "; else cout<<t->data; t=l; } } else { t=t->rchild; if((!t->lchild&&!t->rchild)) { if(t->data=='{') cout<<" "; else cout<<t->data; t=l; } } } cout<<endl; } int main() { printf("请选择下列菜单:\n"); printf("***********************************\n"); printf("******** 1.输入HuffmanTreer的参数 ********\n"); printf("******** 2.初始化HuffmanTreer的参数********\n"); printf("******** 3.创建HuffmanTreer和编码表********\n"); printf("******** 4.输出编码表 ********\n"); printf("******** 5.输入编码,并翻译为字符 ********\n"); printf("******** 6.输入字符,并实现转码 ********\n"); printf("******** 7.退出 ********\n"); printf("***********************************\n"); printf("请输入你的选择:\n"); while(1) { int n; cin>>n; switch(n) { case 1: a1();break; case 2: a2();break; case 3: a3(tre[0],0,0);break; case 4: a4();break; case 5: a5();break; case 6: a6(tre[0]);break; case 7: exit (0); } } return 0; }