#include
Typedef struct {unsigned int} htnode, * huffmantree;
Typedef char ** huffmancode;
Void Select (Huffmantree H, INT I, Unsigned INT & S1, Unsigned INT & S2) {INT K = 0; for (int M = 1; M
Void Huffmancoding (HUFFMANTREE & HT, HUFFMANCODE & HC, INT * W, INT N) {IF (n <= 1) Return; INT M = 2 * N-1; HT = (HUFFMANTREE) Malloc ((M 1) * SizeOf Htnode)); int i = 1; for (Huffmantree P = Ht; i <= n; i, p, w) {(* p) .weight = * w; (* p) .parent = 0; (* p) .lchild = 0; (* p) .rchild = 0;} for (; i <= m; i, p) {(* p) .weight = 0; (* p) .parent = 0; (* p) .lchild = 0; (* p) .rchild = 0;} for (i = n 1; i <= m; i) {UNSIGNED INT S1, S2; SELECT (HT, I-1, S1, S2); HT [S1] .parent = i; HT [S2] .parent = I; HT [i] .lchild = S1; HT [i] .rchild = S2; HT [i] .weight = HT [S1] .Weight HT [S2] .weight;} HC = (huffmancode) malloc ((n 1) * sizeof (char *)); char * cd; cd = (char * ) Malloc (N * SizeOf (char)); CD [N-1] = '/ 0'; for (i = 1; i <= n; i) {int start = N-1; for (int C = I, f = HT [i] .parent; f! = 0; c = f, f = HT [f] .parent) IF (ht [f] .lchild == c) CD [- start] = ' 0 '; ELSE CD [- start] =' 1 '; HC [I] = (char *) Malloc ((N-START) * SIZEOF (CHAR)); STRCPY (HC [I], & CD [Start]) Free (CD);
Void main () {char ch [N]; int WW [N]; int N; huffmantree ht; huffmancode hc; cout << "Welcome to Benhafman / decoder, please enter characters and its frequency The system will automatically convert characters to the corresponding Hafman code. "<< Endl; cout <<" Please enter the number of characters you want to compile: "<< Endl; cin >> n; if (n> n || N <1) cout << "exceeds the number of decoding characters!" << endl; if (n> 0 && n