第六周作业—哈夫曼编码实现

来源:互联网 发布:苹果电脑mac怎么截图 编辑:程序博客网 时间:2024/06/05 20:50
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include<iostream>  
  2. #include<stdio.h>  
  3. #include<stdlib.h>  //malloc的头文件,也可以用malloc.h  
  4. #include<string>  
  5. using namespace std;  
  6. int num[26];    //存放个字母出现的次数  
  7. char str[50];  
  8.   
  9. typedef struct{         //哈夫曼树的结构体  
  10.     char ch;  
  11.     int weight;              //权值  
  12.     int parent,lchild,rchild;  
  13. }htnode,*huffmanTree;  
  14. typedef char **hfmcode;  
  15.   
  16. void Select(huffmanTree &HT,int n,int *p1,int *p2) //Select函数,选出HT树到n为止,权值最小且parent为-1的2个节点  
  17. {  
  18.     int i,j,x,y;  
  19.     for(j=0;j<n;++j){  
  20.         if(HT[j].parent==-1)  
  21.         {  
  22.         x=j;break;  
  23.         }  
  24.   
  25.     }  
  26.     for(i=j+1;i<n;++i){  
  27.         if(HT[i].weight<HT[x].weight&&HT[i].parent==-1)  
  28.         {  
  29.         x=i;                  //选出最小的节点  
  30.         }  
  31.     }  
  32.   
  33.     for(j=0;j<n;++j) {  
  34.         if(HT[j].parent==-1&&x!=j)  
  35.         {  
  36.             y=j;break;  
  37.         }  
  38.     }  
  39.   
  40.     for(i=j+1;i<n;++i)  
  41.     {  
  42.         if(HT[i].weight<HT[y].weight&&HT[i].parent==-1&&x!=i)  
  43.         {  
  44.             y=i;                  //选出次小的节点  
  45.         }  
  46.     }  
  47.     if(x>y){  
  48.         *p1=y;  
  49.         *p2=x;  
  50.     }  
  51.     else{  
  52.         *p1=x;  
  53.         *p2=y;  
  54.     }  
  55. }  
  56.   
  57. void hfmcoding(huffmanTree &HT,hfmcode &HC,int w[],int n) //构建哈夫曼树HT,并求出n个字符的哈夫曼编码HC  
  58. {  
  59.     int i,start,c,f,m;  
  60.     int p1,p2;  
  61.     char *cd;  
  62.     if(n<=1){  
  63.        return;  
  64.     }  
  65.     m=2*n-1;  
  66.     HT=(huffmanTree)malloc((m+1)*sizeof(htnode));  
  67.   
  68.     for(i=0;i<n;++i)            //初始化n个叶子结点  
  69.     {  
  70.         HT[i].ch=str[i];  
  71.         HT[i].weight=w[i];  
  72.         HT[i].parent=-1;  
  73.         HT[i].lchild=-1;  
  74.         HT[i].rchild=-1;  
  75.     }  
  76.   
  77.     for(;i<m;++i)        //初始化其余的结点  
  78.     {  
  79.         HT[i].weight=-1;  
  80.         HT[i].parent=-1;  
  81.         HT[i].lchild=-1;  
  82.         HT[i].rchild=-1;  
  83.     }  
  84.   
  85.     for(i=n;i<m;++i)        //建立哈夫曼树  
  86.     {  
  87.         Select(HT,i,&p1,&p2);  
  88.         HT[p1].parent=i;HT[p2].parent=i;  
  89.         HT[i].lchild=p1;HT[i].rchild=p2;  
  90.         HT[i].weight=HT[p1].weight+HT[p2].weight;  
  91.     }  
  92.     HC=(hfmcode)malloc((n+1)*sizeof(char *));  
  93.     cd=(char *)malloc(n*sizeof(char));  
  94.     cd[n-1]='\0';  
  95.     for(i=0;i<n;++i)              //给n个字符编码  
  96.     {  
  97.         start=n-1;  
  98.         for(c=i,f=HT[i].parent;f!=-1;c=f,f=HT[f].parent)  
  99.         {  
  100.             if(HT[f].lchild==c)  
  101.             {  
  102.                 cd[--start]='0';  
  103.             }  
  104.             else{  
  105.                 cd[--start]='1';  
  106.             }  
  107.         }  
  108.         HC[i]=(char*)malloc((n-start)*sizeof(char));  
  109.         strcpy(HC[i],&cd[start]);  
  110.     }  
  111.     free(cd);  
  112. }  
  113.   
  114. void main()  
  115. {  
  116.     int i,j,t=-1;  
  117.     huffmanTree HT;  
  118.     hfmcode HC;  
  119.     int w[50];  
  120.     string data;//也可以用数组,但是用数组要给定数组的大小,数组长度的表示为strlen(data)  
  121.     string str1;  
  122.     int index=-1,n=0;  
  123.     char c;  
  124.     //cout<<"请输入字符串: ";  
  125.     //cin>>data;//输入字符串  
  126.     data = "Chapter Graphs surveys the most important graph processing problems  including depth-first search breadth first search minimum spanning trees and shortest paths";  
  127.     for( i=0;i<data.length();i++) {  
  128.         if(data[i]>='a'&&data[i]<='z')  
  129.            index=data[i]-'a';  
  130.         else if(data[i]>='A'&&data[i]<='Z')  
  131.             index=data[i]-'A';  
  132.         if(index!=-1)  
  133.             num[index]++;  
  134.             index=-1;  
  135.     }  
  136.     cout<<"各个字母出现的次数分别为:"<<endl;  
  137.     for( j=0;j<26;j++)//输出  
  138.     {  
  139.         if(num[j]!=0)  
  140.         {  
  141.             n++;t++;  
  142.             c='a'+j;  
  143.             w[t]=num[j];//把个字母出现的次数复制到数组w中,作为每个叶子节点的权值  
  144.             str[t]=c;  
  145.             cout<<c<<" : "<<num[j]<<endl;  
  146.         }  
  147.     }  
  148.     hfmcoding(HT,HC,w,n);  
  149.     cout<<"各字母对应的哈夫曼编码分别为:"<<endl;  
  150.     for(i=0;i<n;++i)  
  151.     {  
  152.         cout<<HT[i].ch<<":"<<HC[i]<<endl;  
  153.     }  
  154.     int k=2*n-2;  
  155.     cout<<"请输入二进制码: ";  
  156.     cin>>str1;  
  157.     cout<<"对应字母为: ";  
  158.     for(i=0;i<str1.length();i++)  
  159.     {  
  160.           
  161.         if(str1[i]=='0')  
  162.         {  
  163.             k=HT[k].lchild;  
  164.             if(HT[k].lchild==-1&&HT[k].rchild==-1)  
  165.             {  
  166.                 cout<<HT[k].ch;  
  167.                 k=2*n-2;  
  168.             }  
  169.         }  
  170.         else if(str1[i]=='1')  
  171.         {  
  172.             k=HT[k].rchild;  
  173.             if(HT[k].lchild==-1&&HT[k].rchild==-1)  
  174.             {  
  175.                 cout<<HT[k].ch;  
  176.                 k=2*n-2;  
  177.             }  
  178.         }  
  179.   
  180.     }  
  181.     cout<<endl;  
  182.     
  183. }                   


运行结果:





0 0