I have compiled a Herffman compiler, that is, I can't run, I want to hit the wall, please help me adjust. Thank you all the source code:
Typedef struct; unsigned int parent, lchild, rchild;} htnode, * huffmantree; // dynamically assigned array storage herffman tree typef char ** huffmancode; // dynamically assigned array storage Hafman coding table
#include
Void Huffmancoding (HUFFMANTREE & HT, HUFFMANCODE & HC, INT * W, INT N) {// Waves the weight of the n characters, constructs the Herff Man tree HT, and find N characters Herffman encoding HC Int M, I, start, s1, s2; unsigned int C, f; huffmantree p; char * cd; if (n <= 1) return; m = 2 * n 1; ht = (huffmantree) malloc ((m 1) * sizeof (htnode)); // 0 unit is empty, start storage information from No. 1 for (p = HT 1, i = 1; i <= n; i , p , w ) {(* P). Weight = * w; (* p) .lchild = (* p) .parent = (* p) .rchild = 0;} for (; i <= m; i, p) (* p). LCHILD = (* p) .rchild = (* p) .rchild = (* p) .Weight = 0; // complete the information of the binary tree, use the algorithm in the textbook, set two layers of cycles, establish Hfffman Tree, / The / algorithm is the smallest of the leaf node of 0 in sequence, stored in S1, S2, the outer layer starts from // i = n 1, find the weight of K = [1 ... I-1] For the smallest node as the new node, set the nodes // of the already used, lchild, rchild for (i = n 1; i <= m; i ) {for (int K = 1; k ht [k] .weight) {s1 = k; mm = HT [k] .weight;}} HT [S1] .parent = i; ht [i] .lchild = S1; for (k = 1; K Ht [k] .weight) {s2 = k; nn = HT [k] .weight;}} HT [S2] .parent = i; ht [i] .rchild = s 2; HT [i] .Weight = HT [S1] .Weight HT [S2] .weight; // Refers the weight of the generated node} // from the leaves to the root reverse summation of HCmann encoded HC = (Huffmancode) Malloc ((N 1) * Sizeof (char *)); // Assign N-character encoded head pointer vector ([0] no) CD = (char *) Malloc (N * Sizeof (char) ); // Distribute the workspace CD [N-1] = '/ 0'; // Code end value for (i = 1; i <= n; i ) {// Character-seek coding START = N-1; // Encoding end character position for (c = i, f = Ht [i] .parent; f! = 0; c = f, f = HT [f] .parent) // from leaves to With the reverse seek Hafman code, the end conditions are f = 0, the Parent of the root node is 0 IF (HT [f] .lchild ==
c) CD [- start] = '0'; // Left 0 right 1 else cd [- start] = '1'; HC [i] = (char *) Malloc ((n-start) * Sizeof CHAR); / / To encode allocate space Strcpy (HC [i], & cd [start]) for the i-th character; // Copy the coded string from the CD to HC} Free (CD); // Release the work space} void main () // Number N of the weight and weight from the keyboard, the output of Herffmancode Ht; Huffmancode Ht; INT * W, N, I; COUT << "please enter the weight. Number: / n "; cin >> N; w = (int *) malloc (n * sizeof (int)); cout <<" Please enter the weight: / n "in turn: / n"; for (i = 1; i <= n; i , w ) cin >> * w; w = wn; // w Back to start Huffmancoding (HT, HC, W, N); for (i = 1; i <= n; i ) // cout << * (HC [I]) << '/ n'; PUTS (HC [I]);