赫夫曼编/译码系统

 

赫夫曼编码/译码器

摘要:

   利用赫夫曼编码进行通信可以大大提高信道利用率,缩短信息传输时间,降低传输成本。这要求在 发送端通过一个编码系统对待传输数据预先编码,在接收端将传来的数据进行译码(复原)。对于双工信道(即可以双向传输信息的信道),每端都需要有一个完整的编/译码系统。本文编写了一个简单的赫夫曼码的编/译码小程序。

    本程序主要实现了利用赫夫曼二叉树对一篇英语文档进行编码,对得到的编码利用同一个二叉树解码,最后对解码得到的文档进行测试。经过测试后得出结论,赫夫曼编码虽然提高了信道的利用率,但在信号的保真度上不高,一旦信号出错,整个译码将出错。

实验目的:

   理解Huffman二叉树的概念

   学会使用Huffman编码对数据进行无损压缩的原理

 

 

一、问题描述: 

     利用赫夫曼编码进行通信可以大大提高信道利用率,缩短信息传输时间,降低传输成本。这要求在 发送端通过一个编码系统对待传输数据预先编码,在接收端将传来的数据进行译码(复原)。对于双工信道(即可以双向传输信息的信道),每端都需要有一个完整的编/译码系统。本文编写了一个简单的赫夫曼码的编/译码小程序。

二、设计要求:

   该程序有以下流程:

Initialization(初始化):从输入端输入标准字符集(initdata.dat)和待编码ASCII文件(data.dat),并统计和计算出文章中出现的字符频率,概率和字符数。并计算出信源熵H(entropy)。

EnCoding(编码过程):由输入端输入的各字符出现频率为权值构造赫夫曼二叉树。子叶便是文中出现的各字符。通过根到子叶的路径将其转换为赫夫曼编码。再将源文档中的字符替换为相应的赫夫曼码,并存档(code.dat)。

DeCoding(译码过程):在接收端接收到译码文件(code.dat)后,利用译码过程中生成的赫夫曼二叉树译码。对译码文件扫描,将赫夫曼码替换为原文档,结果存为文档(test.dat)。

Testing(测试):对原文件和译码文件进行校队,相同则编译成功,不相同则编译失败。

Print(打印代码文件):打印内存中的赫夫曼树。各字符对应的编码,编码平均长度,编码效率(信源熵/编码平均长度),显示原文本,编码文本和译码文本。

三、算法描述:

 编码算法:

Input:w[],n//权值数组和数组的大小

Output:HT,HC//赫夫曼二叉树和赫夫曼编码表。

1.由输入的权值数组构造n个赋权的二叉树。

2.在这些二叉树中选取权值最小的两个构造一棵新的二叉树,并删除子叶。

3.如果只剩一棵二叉树,则赫夫曼二叉树构造完毕。否则在剩下的二叉树中返回第二步。

//---从子叶到个逆向求每个字符的赫夫曼编码------

1.从上面过程构造出的二叉树的第一个子叶开始,寻找它的双亲。

2.判断当前结点有无双亲,若无双亲,则全部出栈,并存入赫夫曼编码表吗,下一个子叶结点,转第一步。

3.判断当前结点是其双亲的左孩子还是右孩子,左孩子,则‘0’入栈,右孩子,则‘1’入栈。

   

 

 

 

译码算法:

   Input:code.dat

   Output:test.dat

从code文件的开始到结尾依次读取字符,通过读取到的字符从赫夫曼二叉树的根结点开始,‘0’当前结点换为左孩子,‘1’当前结点换为右孩子,

重复上述过程直到叶子结点,并输出叶子节点所代表的字符到test文件中。

 

 

测试算法:

 Intput;test.dat,data.dat//译码文件和原文件

  Output:true or false

  分别比较两个文件中的字符是否一样,只有全部一样才返回true否则返回false

 

 

四、变量说明:

    #define MAX_CHARS_NUM 57

    #define MAX_ROW 100

    #define MAX_COL 100

   char chars[MAX_CHARS_NUM];//存储标准字符集

   char article[MAX_ROW][MAX_COL];//存储待统计文章

   int frequence[MAX_CHARS_NUM] = {0};//各字符出现的频度

   int size = 0; //文章中出现过的字符数目

   vector<char> appCh;//出现过的字符

   vector<int> appFre;//出现过的字符的频率

   vector<double> probty;//出现过的字符的概率

   double entropy;//信源熵

//=====本变量说明包含在"Initialization.h"中=======

   typedef struct 

{

unsigned int weight;//权值

char         info; //子叶所代表的字符信息

unsigned int parent, lchild, rchild;

}HTNode, *HuffmanTree; // 数组存储的Huffam树

typedef char **HuffmanCode; // 编码表

 

 

 

 

五、函数说明:

  #include "Initialization.h"

过程一:

void initChars(ifstream &);

初始化标准字符集,从文件初始化

void initArt(ifstream &);

初始化文章

void statistic(char (*)[MAX_COL], char *);

统计字符频率

void fillApp(char *, int *);

初始化非零频率字符集

void calcuPro(vector<int> &, vector<double> &);

计算字符概率,信源概率

void calcuEnt(vector<double> &,double &);

计算信源熵

   利用公式-∑p(xi)log2(p(xi))

void readAndFill();

第一个过程的封装函数

 

#include "EnCoding.h"

第二个过程--编码阶段

int min1(HuffmanTree ,int );

取最小值,辅助Select函数

void Select(HuffmanTree &,int ,int &,int &);

选取最小和次小值

void HuffmanCoding(HuffmanTree &, HuffmanCode &,vector<int> &, int );

编码函数

void documentCode(vector<string> &);

创建"code.dat"文件

 

#include "DeCoding.h"

第三个阶段--译码阶段

void decryption(HuffmanTree &);//译码函数

 

#include "Testing.h"

第四个阶段--测试阶段

bool testCoding();//测试函数成功返回True,失败返回False

#include "Print.h"

第五个阶段--打印阶段,将编/译码信息打印到终端

void showHufTree(HuffmanTree &, vector<string> &);

//打印赫夫曼二叉树

内容包含

编号,权值,字符,双亲,左孩子,右孩子,编码

void calcuAverLen(vector<string> &, double &);

计算平均编码长度

void calcuEncodP(double , double , double &);

计算编码效率

公式:编码效率=信源熵/平均编码长度

void showCodeInfo(double , double );

打印编码信息:平均编码长度,编码效率

void showArt();

打印原文档

void showCode(vector<string> &);

打印所得编码

void showTest();

打印译码文件

void test();

打印测试信息

void print(HuffmanTree &, vector<string>  &);

打印阶段的函数封装

 

六、测试:

标准字符集:

abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ(),.'空格'

保存为initdata.dat

原文件data.dat

 

The JSE(TM) Runtime Environment (JRE) is intended for software developers 

and vendors to redistribute with their applications.

The JSE Runtime Environment contains the Java virtual machine, 

runtime class libraries, and Java application launcher that are 

necessary to run programs written in the Java programming language. 

It is not a development environment and does not contain development 

tools such as compilers or debuggers.  For development tools, see the 

JSE Development Kit.*

 

  注意文章结尾处有'*'是作为结束标记,不作为文章字符

 

 

 

 

 

 

 

 

 

字符统计数据:

字符

a

b

c

d

e

f

g

h

i

j

k

l

m

n

o

p

q

频率

30

3

10

13

48

2

7

11

28

0

0

15

15

38

29

12

0

字符

r

s

t

u

v

w

x

y

z

A

B

C

D

E

F

G

H

频率

27

22

36

10

13

3

0

1

0

0

0

0

1

6

1

0

0

字符

I

J

K

L

M

N

O

P

Q

R

S

T

(

)

,

.

空格

频率

1

7

1

0

1

0

0

0

0

3

3

3

2

2

3

4

69

 

程序执行结果:

The Huffman Tree is:

编号    权值    字符    双亲    左孩子  右孩子  编码

1       30      a       62      0       0       1000

2       3       b       43      0       0       0011010

3       10      c       52      0       0       111110

4       13      d       55      0       0       01010

5       48      e       65      0       0       000

6       2       f       40      0       0       10111010

7       7       g       50      0       0       100110

8       11      h       53      0       0       00100

9       28      i       61      0       0       0110

10      15      l       56      0       0       10010

11      15      m       57      0       0       10110

12      38      n       64      0       0       1110

13      29      o       61      0       0       0111

14      12      p       53      0       0       00101

15      27      r       60      0       0       0100

16      22      s       58      0       0       11110

17      36      t       63      0       0       1010

18      10      u       52      0       0       111111

19      13      v       55      0       0       01011

20      3       w       43      0       0       0011011

21      1       y       37      0       0       101111010

22      1       D       37      0       0       101111011

23      6       E       48      0       0       001100

24      1       F       38      0       0       101111100

25      1       I       38      0       0       101111101

29      3       R       44      0       0       0011100

30      3       S       44      0       0       0011101

31      3       T       45      0       0       0011110

32      2       (       40      0       0       10111011

33      2       )       41      0       0       10111100

34      3       ,       45      0       0       0011111

35      4       .       46      0       0       1011100

36      69              68      0       0       110

37      2               41      21      22

38      2               42      24      25

39      2               42      27      28

40      4               46      6       32

41      4               47      33      37

42      4               47      38      39

43      6               48      2       20

44      6               49      29      30

45      6               49      31      34

46      8               51      35      40

47      8               51      41      42

48      12              54      23      43

49      12              54      44      45

50      14              56      7       26

51      16              57      46      47

52      20              58      3       18

53      23              59      8       14

54      24              59      48      49

55      26              60      4       19

56      29              62      10      50

57      31              63      11      51

58      42              64      16      52

59      47              65      53      54

60      53              66      15      55

61      57              66      9       13

62      59              67      1       56

63      67              67      17      57

64      80              68      12      58

65      95              69      5       59

66      110             69      60      61

67      126             70      62      63

68      149             70      36      64

69      205             71      65      66

70      275             71      67      68

71      480             0       69      70

 

编码内容(code.dat):

The code is :

00111100010000011010011100111010011001011101100111101011111111011110011000111001

11111111010100110101100001100011001110010110110010001111110101100001110101011010

11101110011100111000011001011110011001101111011001101110101000011100101000001010

11010111010011101001101111001111011101010100011011100001000001100101000001011000

10010011100101000010011110110

10001110010101100101100011100101001110100111101101010011111001000000101001101111

01010010001100011010111111101000011000110110110101000100110101000100000011001001

101000001010010110010011011111010001010011001111110111101011100

 

00111100010000011010011100111010011001100011100111111111010100110101100001100011

00111001011011001000111111010110000111010101101111100111111010101000011011101111

01101010001000001101001111000010111000110010110110010010101111111000100101101011

0100011111000100011011100000011111110

01001111111110101001101011000011011111010010100011110111101101001001100011010010

01000010001100001111000111111101000111001010110100111100001011100011010000010100

10110010011011111010001010011001111110110100101000111111111011111000100000010011

01010001001000101011010000100000110

11100001111100001111011110100001001011110101101010011111001001111111110110001010

10001111001100100100010110111101100011011010001101010101000011101100110111011010

10001000001101001111000010111000110001010100011110011001001000101101011001101110

100110110100101000111010011011111110001001100001011100110

10111110110101100110111101101110011110101101000110010100000101100010010011100101

10110000111010101100001110010110110010001111110101100001110101011010001110010101

10010100111000111101101110011110101101111100111111010101000011011101100101000001

011000100100111001011011000011101010110

10100111011110010111101101111011111111111000100110100011110110111110011110110001

01011010010000010011110110011101001100101000000110101111111001101001100000100111

10101110011011010111110001110100110010100000101100010010011100101101100001110101

01101010011101111001011110001111111011110000000110101000100000110

10011100111010011001101011110110000101100010010011100101101100001110101011010111

1110011010101011100

The code's average length is :6.16667

The code's encoding productivity is0.707009

Testing ...

Test succeed !

 

上面的信息表明编码的平均字符长度为6.17;编码效率为:70。7%

可见赫夫曼编码的编码效率较高

 

译码结果(test.dat):

 

The test article is:

The JSE(TM) Runtime Environment (JRE) is intended for software developers

and vendors to redistribute with their applications.

The JSE Runtime Environment contains the Java virtual machine,

runtime class libraries, and Java application launcher that are

necessary to run programs written in the Java programming language.

It is not a development environment and does not contain development

tools such as compilers or debuggers.  For development tools, see the

JSE Development Kit.

 

若我们将code.dat中某个信号修改则译码结果将变为:

 

)re JSE(TM) Runtime Environment (JRE) is intended for software developers 

and vendors to redistributcwith their applications.

The JSE Runtime Environment contains the Java virtual machine, 

runtime class libraries, and Java application launcher that are 

necessary to run programs written in the Java programming language. 

It is not a development environment and does not contain development 

tools such as compilers or debuggers.  For development tools, see the 

JSE Development Kit.*

 

 

七、程序清单:

//statement.h包含定义和声明

#include <fstream>

#include <iostream>

#include <math.h>

#include <vector>

#include <string>

#define MAX_CHARS_NUM 100

#define MAX_ROW 100

#define MAX_COL 100

using namespace std;

char chars[MAX_CHARS_NUM];//存储标准字符集

char article[MAX_ROW][MAX_COL];//存储待统计文章

char result[MAX_ROW][MAX_COL];//存储译码文章

int frequence[MAX_CHARS_NUM] = {0};//各字符出现的频度

int size = 0; //文章中出现过的字符数目

vector<char> appCh;//出现过的字符

vector<int> appFre;//出现过的字符的频率

vector<double> probty;//出现过的字符的概率

double entropy;//信源熵

typedef struct 

{

unsigned int weight;

char         info;

unsigned int parent, lchild, rchild;

}HTNode, *HuffmanTree; // 数组存储的Huffam树

typedef char **HuffmanCode; // 编码表

//---函数声明---

#include "Initialization.h"

void initChars(ifstream &);

void initArt(ifstream &);

void statistic(char (*)[MAX_COL], char *);

void fillApp(char *, int *);

void calcuPro(vector<int> &, vector<double> &);

void calcuEnt(vector<double> &,double &);

void readAndFill();

#include "EnCoding.h"

int min1(HuffmanTree ,int );

void Select(HuffmanTree &,int ,int &,int &);

void HuffmanCoding(HuffmanTree &, HuffmanCode &,vector<int> &, int );

void documentCode(vector<string> &);

#include "DeCoding.h"

void decryption(HuffmanTree &);

#include "Testing.h"

bool testCoding();

#include "Print.h"

void showHufTree(HuffmanTree &, vector<string> &);

void calcuAverLen(vector<string> &, double &);

void calcuEncodP(double , double , double &);

void showCodeInfo(double , double );

void showArt();

void showCode(vector<string> &);

void showTest();

void test();

void print(HuffmanTree &, vector<string>  &);

 

//本程序清单包含在"Initialization.h"中

/*为实现统计字符

*给定字符集G{大小写字母∪( ) , . 和space}共26*2+5=57种字符

*先用标准字符集给G赋初值

*将待统计的文章放入article中,用*号标识文章结尾(文章中的字符只能是标准字符集中的字符)

*统计时用标准字符集与待统计字母集比对相等时

*/

 

void initChars(ifstream &_fin)

{

    for(int i = 0; i < MAX_CHARS_NUM; i++)

_fin.get(chars[i]);

}//从initdata。dat初始化标准字符集

 

void initArt(ifstream &_fin)

{   

char ch;

for(int i = 0; i < MAX_ROW; i++)

{

for(int j = 0; j < MAX_COL; j++)

{

  _fin.get(ch);

      article[i][j] = ch;

}

}

}//从data.dat中初始化文档

 

 void statistic(char (*art)[MAX_COL], char *ch)

 {   

    for(int k = 0; k < MAX_CHARS_NUM; k++)

 {

    for (int i = 0; i < MAX_ROW; i++)

{

    for (int j = 0; j < MAX_COL; j++)

{   

     if(art[i][j] == ch[k])

frequence[k]++;

    if(art[i][j] == '*')break;//遇到文章结尾

}

    if(art[i][j] == '*')break;//遇到文章结尾

}

 }

 }//统计文章中出现字符的频率

 

void fillApp(char *ch, int *fre)

{   

appCh.push_back('0');

appFre.push_back(0);

for(int i = 0; i < MAX_CHARS_NUM; i++)

{

       if(fre[i] != 0)

   {

         appCh.push_back(ch[i]);

 appFre.push_back(fre[i]);

 size++;

   }

}

 void calcuPro(vector<int> &fre, vector<double> &pro)//计算概率

 {

 vector<int>::iterator k;

 int sum = 0;

 for (k = fre.begin()+1; k != fre.end(); k++)

 {

 sum += *k;

 }

 for (k = fre.begin(); k != fre.end(); k++)

 {

 pro.push_back((double)*k/sum);

 }

 }

 void calcuEnt(vector<double> &pro,double &ent)//计算信源熵,-∑p(xi)lg2(xi)

 {

 vector<double>::iterator k;

 ent = 0.0;

 for (k = pro.begin()+1; k != pro.end(); k++)

 {

 ent -= *k*(log(*k)/log(2));

 }

 

 }

 void readAndFill() // 字符统计,填写chars,article,frequence

{

ifstream fin;

fin.open("initdata.dat");

initChars(fin);

    fin.close();

ifstream _fin;

_fin.open("data.dat");

initArt(_fin);

    _fin.close();

    statistic(article,chars);

fillApp(chars,frequence);

calcuPro(appFre,probty);

calcuEnt(probty,entropy);

}

 

//本程序清单包含在"EnCoding.h"文件中

 

int min1(HuffmanTree t,int i)

 { // 函数void select()调用 

   int j,flag;

   unsigned int k; // 取k为不小于可能的值 

   for(j=1;j<=i;j++)

     if(t[j].weight<k&&t[j].parent==0)

       k=t[j].weight,flag=j;

   t[flag].parent=1;

   return flag;

 }

 

 void Select(HuffmanTree &t,int i,int &s1,int &s2)

 { // s1为最小的两个值中序号小的那个 

   int j;

   s1=min1(t,i);

   s2=min1(t,i);

   if(s1>s2)

   {j=s1;s1=s2;s2=j;}

 }

void HuffmanCoding(HuffmanTree &HT, HuffmanCode &HC,vector<int> &appFre, int n)

{// w存放n个字符的权值,构造HuffmanTree HT,并求出n个字符的HuffmanCode HC

    if(n <= 1)return;

int m = 2*n - 1;

int i;

vector<int>::iterator w = appFre.begin()+1;

vector<char>::iterator q = appCh.begin()+1;

unsigned int c, f;

char *cd;

 

HuffmanTree p;

HT = (HuffmanTree)malloc((m+1)*sizeof(HTNode)); //0 号单元未用

    for (p = HT+1, i = 1; i <= n; i++,p++,w++,q++)  

{

    p->weight = *w;

p->info = *q;

    p->lchild = 0;

    p->parent = 0;

    p->rchild = 0;

}    //*p = {*w,0,0,0};//创造n棵树

for (; i <= m; i++, p++) {p->weight = 0;p->lchild = 0;p->parent = 0;p->rchild = 0;}//*p = {0,0,0,0};

int s1, s2;

    for (i = n+1; i <= m; i++)

       {// 在HT[1,...,i-1]选择parent为0且weiht最小的两个结点,起序号分别为s1,s2

    Select(HT, i-1, s1, s2);

    HT[s1].parent = HT[s2].parent = i;

    HT[i].lchild = s1;

    HT[i].rchild = s2;

    HT[i].weight = HT[s1].weight + HT[s2].weight;

       }

 

 

    // 从子叶到根,逆向求出每个字符的HuffmanCode

   HC = (HuffmanCode)malloc((n+1)*sizeof(char *)); 

   cd = (char *)malloc(n*sizeof(char));            // 分配求编码的工作空间

   cd[n-1] = '/0';

   for (i = 1; i <=n; i++)

   {

 int start = n-1;

 for (c = i, f = HT[i].parent; f != 0;c = f, f = HT[f].parent)

 if (HT[f].lchild == c)cd[--start] = '0';

 else cd[--start] = '1';

  HC[i] = (char *)malloc((n-start)*sizeof(char)); // 为第i个字符分配存储空间

 strcpy(HC[i], &cd[start]);

   }

   free(cd); // 释放工作空间

}

void documentCode(vector<string> &HC)

{  

   ofstream fout;

   fout.open("code.dat");

   vector<char>::iterator q;

vector<string>::iterator p = HC.begin()+1;

int i, j, k;

  for(i = 0; i < MAX_COL; i++)

  {

 for(j = 0; j <MAX_ROW; j++)

 {

 for(k = 0, q = appCh.begin()+1; q != appCh.end()+1; k++,q++)

 {

                if(article[i][j] == *q)

fout << *(p+k);

else if(article[i][j] == '/n')

{

fout << endl;

break;

}

 }

 if(article[i][j] == '*')break;

 }

 if(article[i][j] == '*'){fout << '*';break;}

  }

 fout.close();

}

//本程序清单包含在"DeCodiong.h"中

void decryption(HuffmanTree &T)

char c;

ifstream fin;

ofstream fout;

fin.open("code.dat");fout.open("test.dat");

HuffmanTree p;//工作指针

unsigned int m = 2*size - 1;// 根节点地址

    fin.get(c);

while(c != '*')

   p = T + m;

   while(p->lchild&&p->rchild)

   {

 switch(c)

 {

 case'0':{p = T + p->lchild;break;}

 case'1':{p = T + p->rchild;break;}

 case'/n':{fout << "/n";break;}

 }

 fin.get(c);

   }

   fout << p->info;

}

fout << '*';

}

//本程序清单包含在"Testing.h"文件中

bool testCoding()

{

ifstream fin1("data.dat");ifstream fin2("test.dat");

char ch1, ch2;

fin1.get(ch1);

fin2.get(ch2);

while(ch1 != '*')

{

if(ch1 != ch2)

return false;

else

fin1.get(ch1);

fin2.get(ch2);

}

return true;

}

void showHufTree(HuffmanTree &HT, vector<string> HC)

{

cout<<"The Huffman Tree is:"<<endl;

cout<<"编号"<<"/t"<<"权值"<<"/t"<<"字符"<<"/t"<<"双亲"<<"/t"<<"左孩子"<<"/t"<<"右孩子"<<"/t"<<"编码"<<endl;

for(int i = 1; i <= size; i++) cout<<i<<"/t"<<(HT+i)->weight<<"/t"<<(HT+i)->info<<"/t"<<(HT+i)->parent<<"/t"<<(HT+i)->lchild<<"/t"<<(HT+i)->rchild<<"/t"<<HC[i]<<endl;

for(int k = size+1; k <= 2*size-1; k++)

cout<<k<<"/t"<<(HT+k)->weight<<"/t"<<(HT+k)->info<<"/t"<<(HT+k)->parent<<"/t"<<(HT+k)->lchild<<"/t"<<(HT+k)->rchild<<"/t"<<endl;

}

void calcuAverLen(vector<string> HC, double &averLen)

{

vector<int> length; length.push_back(0);

for(int i = 1; i <= size; i++)

length.push_back(HC[i].length());

for(int k = 1; k <= size; k++)

averLen += (double)length[k]/size;

}

void calcuEncodP(double ent, double averLen, double &EncodP)

{

EncodP = ent/averLen;

}

void showCodeInfo(double averLen, double EncodP)

{

cout << "The code's average length is :" << averLen << endl;

cout << "The code's encoding productivity is" << EncodP << endl;

}

void showArt()

{

cout << "The article is :" << endl;

for (int i = 0; i < MAX_ROW; i++)

{

for (int j = 0; j < MAX_COL; j++)

{   

cout << article[i][j];

if(article[i][j] == '*')break;

}

if(article[i][j] == '*')break;//遇到文章结尾

}

cout << endl;

}

 

void showCode(vector<string> &HC)

{

cout << "The code is :" << endl;

vector<char>::iterator q;

vector<string>::iterator p = HC.begin()+1;

int i, j, k;

  for(i = 0; i < MAX_COL; i++)

  {

 for(j = 0; j <MAX_ROW; j++)

 {

 for(k = 0, q = appCh.begin()+1; q != appCh.end()+1; k++,q++)

 {

 

                if(article[i][j] == *q)

cout << *(p+k);

else if(article[i][j] == '/n')

{

cout << endl;

break;

}

 

 }

 if(article[i][j] == '*')break;

 }

 if(article[i][j] == '*')break;

  }

  cout << endl;

}

 

void showTest()

{

cout << "The test article is: " << endl;

ifstream fin("test.dat");

char c;

fin.get(c);

while (c != '*')

{

cout << c;

fin.get(c);

}

cout << endl;

}

 

void test()

{   

cout << "Testing ..." << endl;

if(testCoding())

cout << "Test succeed !" << endl;

else 

cout << "Test fial !" << endl;

showTest();

}

 

void print(HuffmanTree &HT, vector<string> &HC)

{

showHufTree(HT,HC);//显示赫夫曼二叉树

showArt();//显示原文档

    showCode(HC);//显示编码

double averLen = 0.0;//平均编码长度

double EncodP = 0.0;//编码效率

calcuAverLen(HC,averLen);//计算平均编码长度

calcuEncodP(entropy,averLen,EncodP);//计算编码效率

showCodeInfo(averLen,EncodP);

test();

}

#include "statement.h"

 

void main()

{

readAndFill();

HuffmanTree HT;

HuffmanCode HC;

HuffmanCoding(HT,HC,appFre,size);

vector<string>_HC;

_HC.push_back("0");

for(int i=1;i<=size;i++)

     _HC.push_back(HC[i]);

documentCode(_HC);

decryption(HT);

    print(HT,_HC);

}

     


     

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值