#include
#include
#include
#include
#include
using namespace std;
30//# define maxleaf define most of the leaf node
# define len 20
Typedef struct {//tree structures hoffman
char ch;
Double weight;//has a weight
Int the parent;
Int lchild;
Int rchild;
} hnode, * hfmtree;
Typedef hnode Huffman [maxleaf * 2-1].
Huffman h;//define a structure array
Int leaves;
Typedef struct {//used to store the Huffman encoding
Int start;
Int bit [len];
} hcode;
Typedef hcode huffcode [maxleaf];
Huffcode code;
//initializes the Huffman tree, two algorithms, reference books and merge
Void Initialization () {
//the book algorithm 1
Cout<& lt;" The number of input nodes, ";//the number of input leaf node
Cin> Leaves;
int i,j;
for(i=0; i
H [I] lchild=1;
H [I]. Parent=1;
H [I] rchild=1;
H [I]. Weight=0;
}
for(i=0; i
H [I]. Ch=getchar ();
Cin> H [I]. Weight;
}
for(i=0; i
Int m1_pos m2_pos;//for minimum weight, m1 m2 storage time small weights
M1=m2=65536;
M1_pos=m2_pos=0;//m1 for minimum weight of corresponding subscript, little weight subscript m2 storage time
for(j=0; j
M1=h [j]. Journal of weight;
M2_pos=m1_pos;
M1_pos=j;
}
Else if (h [j]. Journal of weight
M2_pos=j;
}
}
H [r]. Leaves + I parent=1;//generates new root, no parents
H [r]. Leaves + I lchild=m1_pos;//the new root left the child in the array subscript
H [r]. Leaves + I rchild=m2_pos;//the new root right of children in the array subscript
H [m1_pos]. Parent=leaves + I;//the original root father position
H [m2_pos]. Parent=leaves + I;//the original root father position
H [r]. Leaves + I weight=m2 + m1;
If (m2==65536) break;
}
//the book algorithm 2
Int p, c;
Hcode hf;//hoffman coding, each leaf node coding
Fstream file (" hfmTree. TXT ", the ios: : out);
for(i=0; i
P=h [I] the parent;
Hf. Start=len - 1;
while(p!=1) {
If (h [p]. Lchild==c) {
Hf. Bit [hf. Start]=0;
}
The else {
Hf. Bit [hf. Start]=1;
}
- hf. Start;
C=p;
P=h [c]. The parent;
}
File
File
}
File. The close ();
Cout
}
//input character, used to achieve Huffman encoding
Void Encoding () {
Cout<& lt;" Please enter the need to be encoded string: ";
Char m;
Fstream file (" CodeFile. TXT ", the ios: : out);
getchar();
While ((m=getchar ())! )='\ n' {
for(int i=0; i
For (int j=code [I] start; j
break; }
}
}
File. The close ();
Cout<& lt;" "& lt;
}
//the input character to realize decoding
Void decoding () {
Fstream fin (" Codefile. TXT ", the ios: : in);//read to decode the file
Fstream fout (" Textfile. TXT ", the ios: : out);//after decoding the code into the file
Int a=0;
Char c [500], ch=NULL;
Int k=0, m=0, I=0;//will CodeFile. TXT file coding read
Fin> c;
for(i=0; i
Char other [20]="";//used to store each leaf node coding, comparison
Fstream temp (" tem. TXT ", the ios: : out);//used to convert plastic coding into character, comparison
For (k=code [I]. Start; K
} Temp. The close ();
Temp. Open (" tem. TXT ", the ios: : in);//file is used to implement the conversion type and character type
Temp> STR;
K=len - code [I]. Start;
For (int a=0; A
if(! STRCMP (STR, other)) {fout
}
fin.close();
Fout. Close ();
Remove (" tem. TXT ");//delete temporary files
Cout
}
//selected as 1: printing code file
Void the Print () {
Fstream fin (" CodeFile. TXT ", the ios: : in);
Fstream fout (" Codeprint. TXT ", the ios: : out);
char ch;
Int sum=0;
Cout<& lt;" Printing code file output is as follows: "& lt;
Sum++;
Cout
If (sum % 50==0) {cout
fin.close();
Fout. Close ();
Cout
}
Fstream fout (" TreePrint. TXT ", the ios: : out);//set up storage file
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull