/*
* Implementation process: the user first constructs the User-Defined tree using the huffmantree () function, and then * starts from the bottom up (that is, starting from the node where the array serial number is zero) in the main () function) it is determined from the above layer. If it is on the left side of * parent node, it is set to 0. If it is on the right side, it is set to 1. Finally, the generated encoding is output. * ------------------------------------------------------------------------ */# Include <stdio. h> # include <stdlib. h ># include <cstring> const int maxbit = 100; const int maxvalue = 10000; const int maxleaf = 30; const int maxnode = maxleaf * 2-1; typedef struct {int bit [maxbit]; int start;} hcodetype; // The encoding structure typedef struct {int weight; int parent; int lchild; int rchild; int value;} hnodetype; // node struct // input and initialize Void node_input (hnodetype huffnode [], const Int & N) {int I; for (I = 0; I <n; I ++) {printf ("Please input weight of leaf node % d: \ n", I); scanf ("% d", & huffnode [I]. weight); // The Weight Value huffnode [I]. parent =-1; huffnode [I]. lchild =-1; huffnode [I]. rchild =-1; // The initial value is-1 huffnode [I]. value = I; // The current value is the lower mark. The actual value can be replaced with a letter} void out_put (hcodetype huffcode [], const Int & N) {int I, j; for (I = 0; I <n; I ++) {printf ("% d' S Huffman code is: ", I); For (j = huffcode [I]. start + 1; j <n; j ++) {printf ("% d", huffcode [I]. bit [J]);} printf ("START: % d", huffcode [I]. start); printf ("\ n") ;}} void min_two (hnodetype huffnode [], const Int & N, const Int & I, Int & X1, Int & x2) {Int J, M1, M2; M1 = m2 = maxvalue; // M1 and M2 store two nodes with no parent node and the minimum node weight. // find the two nodes with the minimum and no parent node in all nodes, and merged into a binary tree for (j = 0; j <n + I; j ++) {If (huffnode [J]. weight <M1 & huffnode [J]. parent =-1) {m2 = m1; x2 = x1; M1 = huffnode [J]. weight; X1 = J;} else if (huffnode [J]. weight <M2 & huffnode [J]. parent =-1) {m2 = huffnode [J]. weight; x2 = J ;}}// construct a huffmantree (hnodetype huffnode [], const Int & N) {// I: cyclic variable, m1 and M2: Construct the weights of the two minimum weight nodes in different processes of the Harman tree. // X1 and X2: construct the sequence numbers of the two least weighted nodes in the array during the different process of the user tree. Int I, x1, x2; // cyclically construct the Huffman tree for (I = 0; I <n-1; I ++) {x1 = x2 = 0; min_two (huffnode, n, i, x1, x2);/* set the parent node information of the two child nodes x1 and X2 */huffnode [X1]. parent = N + I; huffnode [X2]. parent = N + I; huffnode [n + I]. weight = huffnode [X1]. weight + huffnode [X2]. weight; huffnode [n + I]. lchild = x1; huffnode [n + I]. rchild = x2; huffnode [n + I]. parent =-1; // The Father of the new node is-1 printf ("x1.weight and x2.weight in round % d: % d, % d \ n", I + 1. huffnode [X1]. weight, huffnode [X2]. weight);/* for testing */printf ("\ n");}/* for (I = 0; I <n + 2; I ++) {printf ("Parents: % d, lchild: % d, rchild: % d, value: % d, weight: % d \ n", huffnode [I]. parent, huffnode [I]. lchild, huffnode [I]. rchild, huffnode [I]. value, huffnode [I]. weight);} * // test} // end huffmantree // encode void encodeing (hnodetype huffnode [], hcodetype huffcode [], const Int & N) {int I, j, C, p; hcodetype CD; for (I = 0; I <n; I ++) {CD. Start = n-1; C = I; P = huffnode [C]. parent; while (P! =-1) // The parent node has {If (huffnode [p]. lchild = c) CD. bit [cd. start] = 0; else CD. bit [cd. start] = 1; CD. start --; // evaluate the encoding of a lower C = P; P = huffnode [C]. parent; // set the next loop condition} // end while // Save the start position of the Harman encoding and encoding for (j = CD. start + 1; j <n; j ++) {huffcode [I]. bit [J] = CD. bit [J];} huffcode [I]. start = CD. start ;}// decode void decodeing (char string [], hnodetype Buf [], int num) {int I, TMP = 0, code [1024]; int m = 2 * Num-1; char * num P; char num [1024]; for (I = 0; I <strlen (string); I ++) {If (string [I] = '0 ') num [I] = 0; else if (string [I] = '1') num [I] = 1; else return;} // convert it to a number I = 0; nump = & num [0]; while (nump <(& num [strlen (string)]) {TMP = m-1; while (BUF [TMP]. lchild! =-1) & (BUF [TMP]. rchild! =-1) {If (* nump = 0) {TMP = Buf [TMP]. lchild;} else TMP = Buf [TMP]. rchild; nump ++;} printf ("% d", Buf [TMP]. value) ;}} int main (void) {hnodetype huffnode [maxnode]; // defines a node struct array hcodetype huffcode [maxleaf], CD; // defines an encoding struct array, define a temporary variable to store the int I, j, N; char PP [100]; printf ("Please input N: \ n") for encoding "); scanf ("% d", & N); // input n nodes node_input (huffnode, n); // construct the huffmantree (huffnode, n); // encode the encode Ing (huffnode, huffcode, n); // output all the stored Harman encoding out_put (huffcode, n); // The decoding process printf ("decoding? Please enter code: \ n "); scanf (" % s ", & PP); decodeing (PP, huffnode, n); Return 0 ;}
Construction and coding and decoding of the Harman tree