huffman编码和译码实现

写数据结构的实验确实是蛮麻烦的,下面提供一个还可以运行的源程序给大家参考,应付实验老师是绰绰有余的了

link.h文件内容:

#pragma once
class link

{
private:

public:
 link *lchild,*rchild,*parent;
 int data;
 int weight;
 link(void);
 ~link(void);
};

//link的实现文件什么内容也没有,所以就不写啦

huffman.h内容:

#pragma once
#include "link.h"
class huffman:public link
{

public:
 char ch[100];
 link _link[100];
 link *head;
 int def[130];//贮存字母的出现频率
 void redef();//重新定义字母的出现频率
 void sort();//对出现频率排序
 void built();//建立huffman树
 void input();//输入
 void str();//对输入的输入的字符串进行编码
 void decode();//对输入的编码进行译码
 huffman(void);
 ~huffman(void);
};

huffman.cpp文件

#include "StdAfx.h"
#include "huffman.h"
#include<iostream>
using namespace std;

 

//构造函数,预定义编码
huffman::huffman(void)
{
 for(int i=0;i<26;i++)
 {
  def[i+97]=i;
 }
 head=NULL;
 built();
}
//重新定义各个字母的频率
void huffman::redef()
{
 cout<<"请请输入字母以及相应需要改动的权值,输入  ~0  代表输入结束:"<<endl;
 char _char[26];
 int _weight[26];
 int i=0;
 do
 {
  cin>>_char[i];
  cin>>_weight[i];
  i++;
 }while(_char[i-1]!='~');
 for(int j=0;j<i;j++)
 {
  def[int(_char[j])]=_weight[j];
 }
 built();

//建立huffman树
void huffman::built()
{
 int _weight1[26];
 int count1=0;

 int _char[26];
 for(int i=0;i<26;i++)
 {
  _weight1[i]=def[i+97];
  _char[i]=i+97;
 }
 for(int i=0;i<26;i++)
 {
  for(int j=i+1;j<26;j++)
  {
   if(_weight1[j]<=_weight1[i])
   {
    int tt=_char[i];
    int ss=_weight1[i];
    _weight1[i]=_weight1[j];
    _weight1[j]=ss;
    _char[i]=_char[j];
    _char[j]=tt;  
   }
  }
 }
 //对权值进行排序

 _link[0].data=_char[0];
 _link[0].lchild=NULL;
 _link[0].rchild=NULL;
 _link[0].parent=&_link[2];

 _link[0].weight=_weight1[0];

 _link[1].data=_char[1];
 _link[1].lchild=NULL;
 _link[1].rchild=NULL;
 _link[1].parent=&_link[2];
 _link[1].weight=_weight1[1];


 _link[2].data=NULL;
 _link[2].lchild=&_link[0];
 _link[2].rchild=&_link[1];
 _link[2].parent=&_link[4];
 _link[2].weight=_weight1[0]+_weight1[1];
 int count=2;
 int i;
 
 for(i=3;i<51;i++)
 {
  if(i%2==1)
  {

   _link[i].data=_char[count];
   _link[i].lchild=NULL;
   _link[i].rchild=NULL;
   _link[i].parent=&_link[i+2];
   _link[i].weight=_weight1[count];
   count++;
  }

  else
  {

   _link[i].data=NULL;
   if(_link[i-1].weight > _link[i-2].weight)
   {
    _link[i].rchild=&_link[i-2];
    _link[i].lchild=&_link[i-1];
   }
   else
   {
    _link[i].rchild=&_link[i-1];
    _link[i].lchild=&_link[i-2];
   }
   _link[i].parent=&_link[i+1];
  }   
 } 
 head=&_link[50];

}

void huffman::input()
{
  char ch;
 for(int j=97;j<=122;j++)
 {
  ch=(char)j;
  cout<<ch<<"的编码是:"<<endl;
  int code[1000],i=0;;
  link *p=head;

  while(1)
  {
   if(p->lchild->data==(int)ch)
   { 
    code[i]=0;
    i++;
    for(int j=0;j<i;j++)
    {
     cout<<code[j];
    }
    cout<<endl;
    break;
   }
   if(p->rchild->data==(int)ch)
   {
    code[i]=1;
    i++;
    
    for(int j=0;j<i;j++)
    {
     cout<<code[j];
    }
    cout<<endl;
    break;
   }
   if(p->lchild->data==NULL)
   {
    code[i]=0;
    p=p->lchild;
   }
   if(p->rchild->data==NULL)
   {
    code[i]=1;
    p=p->rchild;
   }
   i++;
  }
 } 
}
//输出输入字符串的编码
void huffman::str()
{
 char st[200];
 cout<<"请输入字符串:"<<endl;
 cin>>st;
 cout<<"编码是:"<<endl;
 int count=0;

 do
 {
  char ch = st[count];
  int code[1000],i=0;;
  link *p=head;
  while(1)
  {
   if(p->lchild->data==(int)ch)
   {
    code[i]=0;
    i++;
    for(int j=0;j<i;j++)
    {
     cout<<code[j];
    }
    break;
   }
   if(p->rchild->data==(int)ch)
   {
    code[i]=1;
    i++;
    for(int j=0;j<i;j++)
    {
     cout<<code[j];
    }
    cout<<endl;
    break;
   }
   if(p->lchild->data==NULL)
   {
    code[i]=0;
    p=p->lchild;
   }
   if(p->rchild->data==NULL)
   {
    code[i]=1;
    p=p->rchild;
   }
   i++;
  }
  count++;
 }while(st[count]!=NULL);
 cout<<endl;
 cout<<endl;
 cout<<endl;
 cout<<endl;
}

//对编码进行译码
void huffman::decode()
{
 char code[10000];
 int count = 0;
 int next=0;
 link *p=head;
 cout<<"请输入合法的编码:"<<endl;
 cin>>code;
 cout<<"所代表的字符串是:"<<endl;
 while(code[count]!=NULL)
 {
  next = (int)code[count]-48;
  //cout<<next<<"zu"<<endl;
  if(next==0||next==1)
  {
   if(next==0)
   {
    p=p->lchild;
    if(p->data!=NULL)
    {
     cout<<(char)p->data;
     p=head;
    }
   }
   else
   {
    p=p->rchild;
    //cout<<"检测到"<<endl;
    if(p->data!=NULL)
    {
     cout<<(char)p->data;
     p=head;
    }
   }
   count++;
  }
  else
  {
   cout<<endl;
   cout<<"你输入的编码不正确!"<<endl;
   break;
  }
 }
 cout<<endl;
 cout<<endl;
 cout<<endl;
}

主文件:

 

#include "stdafx.h"
#include "huffman.h"
#include<iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
 char a;
 huffman hfm;
 while(1)
 {
  cout<<"****************************************"<<endl;
  cout<<"*   请选择:                           *"<<endl;
  cout<<"*          1.重新定义使用频率          *"<<endl;
  cout<<"*          2.查询字母编码              *"<<endl;
  cout<<"*          3.输入字符串查看编码        *"<<endl;
  cout<<"*          4.输入编码进行译码          *"<<endl;
  cout<<"*          5.退出                      *"<<endl;
  cout<<"****************************************"<<endl;
  cin>>a;

  int i = (char)a - 48;
  switch(a)
  {
  case 1:hfm.redef();break;
  case 2:hfm.input();break;
  case 3:hfm.str();break;
  case 4:hfm.decode();break;
  case 5:exit(1);break;
  default:cout<<"输入不正确!"<<endl;break;
  }
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值