<Span style = "color: # 6600cc;" >#include <stdio. h> # include <string. h> # include <stdlib. h> typedef struct {char a; // record int weight; // weight int parent, lchild, rchild;} htnode, * huffmantree; typedef char ** huffmancode; // dynamically allocate the Array Storage table void select (huffmantree HT, int M, int * S1). // select the node with the smallest parent node and the smallest weight {int I, j, k, H, a, B; for (a = 1; A <= m; A ++) if (HT [A]. parent = 0) break; I = A; k = A; For (; I <= m; I ++) if (HT [I]. weight <HT [K]. weight & HT [I]. Parent = 0) k = I; * S1 = K;} // obtain the huffmancoding (huffmantree * HT, huffmancode * HC, int * w, char * s, int N) {int m, I, start, S1, S2, C, F; char * CD; huffmantree P; If (n <= 1) return; m = 2 * n-1; * ht = (huffmantree) malloc (m + 1) * sizeof (htnode); for (I = 1; I <= N; I ++, W ++, s ++) {(* HT) [I]. weight = * w; // the weight of the leaf node remains unchanged (* HT) [I]. A = * s; (* HT) [I]. parent = 0, (* HT) [I]. lchild = 0, (* HT) [I]. rchild = 0 ;}for (I = n + 1; I <= m; I ++) (* HT) [I]. parent = 0; // Pa Rent initialization for (I = n + 1; I <= m; ++ I) {// create a select (* HT, I-1, & S1); (* HT) [S1]. parent = I; select (* HT, I-1, & S2);/* select parent as 0 and the two knots with the smallest weight as S1 and S2 */(* HT) [s2]. parent = I; (* HT) [I]. lchild = S1; (* HT) [I]. rchild = S2; (* HT) [I]. A = '0'; (* HT) [I]. weight = (* HT) [S1]. weight + (* HT) [s2]. weight;} // reverse evaluate each character from the leaf to the root of the Harman encoding (* HC) = (huffmancode) malloc (n + 1) * sizeof (char *)); /* allocate the header pointer vector of N character encoding */Cd = (char *) malloc (N * sizeof (char); // assign the work interval CD [n-1] = '\ 0'; // encoding Terminator for (I = 1; I <= N; I ++) {// calculate the START = n-1 encoding for each character; // encoding Terminator position for (C = I, F = (* HT) [I]. parent; F! = 0; C = F, F = (* HT) [f]. parent)/* reverse encoding from leaf child to root */If (* HT) [f]. lchild = c) CD [-- start] = '0'; else CD [-- start] = '1'; (* HC) [I] = (char *) malloc (n-Start) * sizeof (char);/* allocates space for the I-character encoding */strcpy (* HC) [I], & CD [start]); // copy from CD to HC} // decodes void huffmandising (huffmantree HT, int N) {file * fp3, * fp4; huffmantree P; int C, M; char B; P = HT; M = 2 * n-1; * P = HT [m]; fp3 = fopen ("file3.txt", "R "); fp4 = fopen ("file4.txt", "W"); If (fp3! = NULL) {fscanf (fp3, "% C", & B);/* reads a character. If it is '0', it traverses the left subtree, traverse the right subtree for '1' and output the leaf node */while (! Feof (fp3) {If (B = '0') {If (* P ). A = '0') C = (* P ). lchild, (* P) = HT [c]; // point to left child} If (B = '1') {If (* P ). A = '0') C = (* P ). rchild, (* P) = HT [c]; // point to right child} If (* P ). a! = '0') fprintf (fp4, "% C", (* P ). a), // leaf node output (* P) = HT [m]; // point to the root node if (! Feof (fp3) fscanf (fp3, "% C", & B) ;}} fclose (fp4); fclose (fp3);} void printfbitree (huffmantree * HT, huffmancode * HC, int N) {int I, j = 1, m = N * 2-1; printf ("serial number character weight parent left child right child Harman Code \ n"); for (I = 1; I <= m; I ++, J ++) {printf ("% 02d % C % 2D % 2D % 2D % 2D", I, (* HT) [I]. a, (* HT) [I]. weight, (* HT) [I]. parent, (* HT) [I]. lchild, (* HT) [I]. rchild); If (j <= N) printf ("% s", (* HC) [J]); printf ("\ n") ;}} int main () {int A [258], N, C [100], I, J, K, Z; c Har E; char M [100], B; huffmantree HT; huffmancode HC; file * FP1, * fp2, * fp3, * fp4; for (I = 0; I <= 256; I ++) A [I] = 0; // read a character. Is the position in array a determined by its ASCII? For (I = 0; I <100; I ++) C [I] = 0; // record the weight of each character FP1 = fopen ("file1.txt", "R"); If (FP1! = NULL) {While (! Feof (FP1) {fscanf (FP1, "% C", & E); If (! Feof (FP1) A [(INT) E] ++; // count the number of occurrences} fclose (FP1); for (I = 0, j = 0; I <= 256; I ++) if (a [I]! = 0) C [J ++] = A [I], M [J-1] = (char) I; // Save the character to the array M huffmancoding (& HT, & HC, C, M, J); FP1 = fopen ("file1.txt", "R"); fp2 = fopen ("file2.txt", "W "); fp3 = fopen ("file3.txt", "W +"); If (FP1! = NULL) // output the corresponding content in file no. 1 to file No. 3 in file 2 {While (! Feof (FP1) {fscanf (FP1, "% C", & E); If (feof (FP1) break; for (I = 0; I <J; I ++) if (M [I] = e) break; fprintf (fp2, "% s", HC [I + 1]); fprintf (fp3, "% s", HC [I + 1]) ;}} fclose (FP1); fclose (fp2); fclose (fp3); printfbitree (& HT, & HC, j); // output related information huffmandising (HT, J); // decoding function} </span>