/* #author: zhuqi,2123419 #date:D EC 11,2014 #algorithm #to Produce Huffman Coding #example: Input (weight): a=45 b=13 c=12 d=16 e=9 f=5 output (Huffman code): a=0 b=101 c=100 d= e=1101 f=1100 * * #include <stdio.h> #define MAXBIT #define MAXVALUE 10000 #define MAXLEAF 30 # Define Maxnode maxleaf*2-1 typedef struct {int bit[maxbit]; int start; CodeType; /* Storing the encoded structure body/typedef struct {int weight; int parent; int lchild; int rchild;} NodeType; /* Storage node of the structure/void Huffmantree (NodeType huffmannode[maxnode], int n) {/*m1, M2: Structured huffman tree in different processes of the two minimum weights of the weights, X1, X2: Structured huffman tree different processes The ordinal number of the two minimum weights nodes in the array.
* * int I, J, M1, M2, X1, x2;
/* Initialize Store Huffman tree array huffnode[] in the node * * for (i = 0; i < 2*n-1; i++) {huffmannode[i].weight = 0;
Huffmannode[i].parent =-1;
Huffmannode[i].lchild =-1;
Huffmannode[i].lchild =-1; for (i = 0; i < n; i++) {char k = (char) (' a ' + (i-0)); printf ("Please input weight of the leaf node%c: \ n"), k);
scanf ("%d", &huffmannode[i].weight);
printf ("--------------------------------------------------\ n");
printf ("The intermediate process: \ n"); /* Cyclic construction Huffman tree/for (i = 0; i < n-1; i++) {m1 = m2 = MAXVALUE;/* M1, M2 store two nodes with no parent nodes with the smallest node weights * * X1 = x2 = 0;/* Find out Two nodes with minimum weights and no parent nodes in all nodes, and merged as a binary tree/for (j = 0; J < N+i; j) {if (Huffmannode[j].weight < M1 && huffmannode[ J].parent==-1) {m2 = m1; x2 = x1; m1 = huffmannode[j].weight; x1 = j;} else if (Huffmannode[j].weight < m2 &&
Huffmannode[j].parent==-1) {m2 = huffmannode[j].weight; x2 = j;}}
/* Set two child nodes found X1, x2 parent Node Information * * huffmannode[x1].parent = n+i;
Huffmannode[x2].parent = N+i;
Huffmannode[n+i].weight = Huffmannode[x1].weight + huffmannode[x2].weight;
Huffmannode[n+i].lchild = x1;
Huffmannode[n+i].rchild = x2; printf ("X1.weight and X2.weight in round%d:%d,%d\n", I+1, Huffmannode[x1].weight, huffmannode[x2].weight);
Displays the selected node printf ("\ n") for each round; int main () {NodeType Huffmannode[maXNode];
CodeType Huffmancode[maxleaf], temp;
int I, J, cur, par, n;
printf (">>>huffman coding<<<\n");
printf ("Please input the number of node: \ n");
scanf ("%d", &n);
Huffmantree (Huffmannode, N); for (i = 0; i < n; i++) {temp.start = n-1; cur = i; par = huffmannode[cur].parent; while (par!=-1) {if (Huffmannod
E[par].lchild = = cur) Temp.bit[temp.start] = 0;
else Temp.bit[temp.start] = 1;
temp.start--;
cur = par;
Par=huffmannode[cur].parent;
}/* Save the Huffman encoding and encoding of each leaf node (j = Temp.start+1 J < N; j + +) {Huffmancode[i].bit[j] = temp.bit[j];}
Huffmancode[i].start = Temp.start;
printf ("--------------------------------------------------\ n");
printf ("After Huffman coding: \ n");
/* Output Huffman encoding/for (i = 0; i < n; i++) {char k = (char) (' a ' + (i-0)); printf ("Node%c's Huffman code is:", K);
for (j = huffmancode[i].start+1 J < N; j +) {printf ("%d", huffmancode[i].bit[j]); printf ("\ n");
return 0; }