* * Huffman code-CHAIN structure * * Function implementation: * Source file character Fu Quan value validation operation * Huffman tree set up operation * Character dictionary set up operations * source file transcoding operation * Binary file decoding operation * File output operation * Memory cleanup operation * * #includ
E <stdio.h> #include <stdlib.h> #define _HFMALGORITHM_ #define CODESIZE 30//Huffman tree node definition typedef struct forest{
struct Forest *left;
struct Forest *right;
struct Forest *parent;
unsigned int weight;
char word;
}hfmnode;
Character dictionary node definition typedef struct hfmtable{char word;
Char Code[codesize];
int start;//Mark starting position struct hfmtable *next;
}table;
function Declaration Hfmnode * ReadFile (void);
Hfmnode * Createhfmtree (Hfmnode *f);
void Hfmcode (Hfmnode * root,table * &table);
void Printhfmtree (Hfmnode *root);
void PrintTable (Table *t);
void transcoding (table * table);
void Retranscoding (Hfmnode * root);
void Selectfile (void);
void Printfileinformation (char filename[]);
void Destroyhfmtree (Hfmnode *root);
void Destroytable (Table *table);
Read the source file, confirm the Fu Quan value of each character to establish the forest Hfmnode * ReadFile (void) {Hfmnode *head,*p;
FILE *FP = NULL;
Char A; Create a header node head = (Hfmnode *) MallOC (sizeof (Hfmnode));
Head->left = Head->right = Head->parent = NULL;
Head->word = ' n ';
head->weight = 0;
Read source file, confirm weight fp = fopen ("Input.data", "R");
while ((A = fgetc (FP))!= EOF} {for (P = head;p->parent && p->parent->word-a;p = p->parent);
if (!p->parent) {//No node is found, append a new node p->parent = (Hfmnode *) malloc (sizeof (Hfmnode));
P->parent->left = P->parent->right = P->parent->parent = NULL;
P->parent->word = A;
p->parent->weight = 0;
}//end if p->parent->weight++;
}//end while Fclose (FP);
return head; Hfmnode * Createhfmtree (Hfmnode *f) {Hfmnode *p,*k1,*k2;//k1,k2 as minimum minor value precursor node Hfmnode *d1,*d2;//d1,d2 for minimum value based on weights Value secondary small value node while (f->parent->parent) {for (K1 = f,p = K2 = F->parent;p->parent;p = p->parent) {//From the list forest Minimum minor value precursor node if (P->parent->weight < k1->parent->weight) {K2 = K1;
K1 = p;
}//end if Else if (P->parent->weight < k2->parent->weight) {K2 = p;
}//end if}//end for//fetch minimum, secondary small value node, after merging into forest D1 = k1->parent;
D2 = k2->parent;
if (d1->parent = = D2) {///////Divide case processing order of nodes k2->parent = d2->parent;
K1->parent = d1->parent;
}//end if else{k1->parent = d1->parent;
K2->parent = d2->parent;
}//end else//Create a transition node P = (Hfmnode *) malloc (sizeof (Hfmnode));
P->left = D1;
P->right = D2;
P->word = ' n ';
P->weight = D1->weight + d2->weight;
d1->parent = D2->parent = P;
P->parent = f->parent;
F->parent = p;
} f->parent->parent = NULL;
p = f->parent;
Free (F);
return p;
}//Establish Huffman character dictionary void Hfmcode (Hfmnode * root,table * &table) {Table *p;
Hfmnode *q,*r;
if (root) {Hfmcode (root->left,table);
Hfmcode (root->right,table); if (Root->left = = NULL && root->right= = NULL) {for (P = table;p && p->word-root->word;p = p->next);
There is no decoding dictionary for the character in the if (!p) {//table, header-inserted dictionary information p = (table *) malloc (sizeof (table));
P->next = table;
Table = P;
P->word = root->word;
P->start = codesize;
P->code[--p->start] = ' the ';
Q = root->parent;
R = root;
while (q) {P->code[--p->start] = ' 0 ' + (q->left!= R);
R = q;
Q = q->parent;
The Haffman node information is exported in the middle sequence traversal void Printhfmtree (Hfmnode *root) {if (root) {printhfmtree (root->left);
if (Root->left = = NULL && root->right = null) printf ("Character:%c Right value:%d\n", root->word,root->weight);
Printhfmtree (Root->right);
}//Output Huffman character dictionary information void printtable (Table *t) {printf ("character \ t huffman code \ n");
for (; t;t = T->next) {printf ("%-4c\t%s\n", T->word,t->code + T->start);
} putchar (' \ n ');
//Read the source file to carry it Huffman transcoding void transcoding (table * table) {file *fp1,*fp2;
Table *p;
Char A; FP1 = fopen ("INput.data "," R ");
FP2 = fopen ("Password.data", "w");
while ((A = fgetc (FP1))!= EOF) {for (P = table;p && p->word-a;p = p->next);
if (!p) {printf ("Found Error in transcoding!\n");
Exit (0);
} fputs (P->code + P->START,FP2);
} fclose (FP1);
Fclose (FP2);
} void Retranscoding (Hfmnode * root) {FILE *fp1,*fp2;
Hfmnode *p;
char a = 0;
FP1 = fopen ("Password.data", "R");
FP2 = fopen ("Output.data", "w");
while (a!= EOF) {p = root;
while (1) {a = Fgetc (FP1);
if (A-' 0 ') p = p->right;
else P = p->left;
if (P->word) {FPUTC (P->WORD,FP2);
Break
}} fclose (FP1);
Fclose (FP2);
} void Printfileinformation (char filename[]) {FILE *fp;
Char A;
Print file printf ("%s content: \ n", FileName);
if (fp = fopen (filename, "r") = = NULL) {printf ("%s open failed!\n", filename);
Exit (0);
while ((A = fgetc (FP))!= EOF) {Putchar (a);
printf ("\ n \ nthe");
Fclose (FP); } void Selectfile (void) {PrintfileInformation ((char *) "input.data");
Printfileinformation ((char *) "password.data");
Printfileinformation ((char *) "output.data");
} void Destroyhfmtree (Hfmnode *root) {if (root) {destroyhfmtree (root->left);
Destroyhfmtree (Root->right);
Free (root);
} void destroytable (table *table) {table *p;
while (table) {p = table;
Table = table->next;
Free (p);
int main (void) {Hfmnode *f;//forest hfmnode *root;//Optimal binary root node Table *table = null;//Huffman decoding dictionary int n;
Read the article from the file to memory, stored in the vector<word> wordtable F = ReadFile ();
printf ("Weight confirmation complete ... \ n");
Based on the above table, the optimal binary tree root = Createhfmtree (F) is established.
printf ("The best binary tree is established ... \ n");
Output hfmtree printf ("Output optimal binary tree valid node: \ n");
Printhfmtree (root);
Putchar (' \ n ');
The establishment of HFM decoding dictionary hfmcode (root,table);
printf ("Decoding dictionary established ... \ n");
Output HFM decoding dictionary printf ("Output decoding dictionary: \ n");
PrintTable (table);
Read into the article for transcoding processing transcoding (table);
printf ("The source file transcoding complete ... \ n");
Binary transmission file decoding processing retranscoding (root);
printf ("Binary file decoding complete ... \ n"); The source file,Binary file, decoding file printed on the DOS screen Selectfile ();
Memory release Destroyhfmtree (root);
Destroytable (table);
printf ("End of program, empty memory complete ... \ n");
return 0; }