Home > Back-end >  Why the last function Huffman tree to print out
Why the last function Huffman tree to print out

Time:12-08

#include//need to use the header file
#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; iH [I]. Ch=0;
H [I] lchild=1;
H [I]. Parent=1;
H [I] rchild=1;
H [I]. Weight=0;
}
for(i=0; iCout<& lt;" Please enter the first "& lt; getchar();
H [I]. Ch=getchar ();
Cin> H [I]. Weight;
}
for(i=0; iDouble m1, m2,
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; jIf (h [j] weightM2 equals m1;
M1=h [j]. Journal of weight;
M2_pos=m1_pos;
M1_pos=j;
}
Else if (h [j]. Journal of weightM2=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; iC=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;
}
FileFor (j=hf start + 1; jCode [I] - [j]=hf. Bit [j];
FileCout} fileCode [I]. Start=hf. Start + 1;
}
File. The close ();
Coutsystem("pause");
}


//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; iIf (m==h [I]. Ch) {
For (int j=code [I] start; jFilebreak;
}
}
}
File. The close ();
Cout<& lt;" "& lt; Coutsystem("pause");
}

//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; iChar STR [50]="";
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; KTemp}
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; AOther [a] [m + a]=c;
if(! STRCMP (STR, other)) {foutCoutTemp. The close ();
}
fin.close();
Fout. Close ();
Remove (" tem. TXT ");//delete temporary files
CoutCoutsystem("pause");
}

//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; While (fin. Get (ch)!=NULL) {//control output character number
Sum++;
CoutFout
If (sum % 50==0) {cout}
fin.close();
Fout. Close ();
Coutsystem("pause");
}
Fstream fout (" TreePrint. TXT ", the ios: : out);//set up storage file
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related