typedefstructNode { int weight; int parent; char value; int lchild, rchild; }HTNode, *HuffmanTree; typedefchar **HuffmanCode;
HuffmanTree create_HuffmanTree(int n); voidselect_minium(HuffmanTree HT, int k, int &min1, int &min2); intmin(HuffmanTree HT, int k); voidHuffmanCoding(HuffmanTree HT, HuffmanCode &HC, int n); intcountWPL(HuffmanTree HT, int n); voidHuffmanDecoding(char ch[], HuffmanTree HT,int n);
#include"head.h" HuffmanTree create_HuffmanTree(int n) { int *wet = (int*)malloc(sizeof(int) *MAXSIZE); char *value = (char*)malloc(sizeof(char)*MAXSIZE); int weight; char v; for (int i = 0; i < n; i++) { cout << "请输入节点" << i << "的值和权重:"; cin >> v; cin >> weight; *value = v; *wet = weight; value++; wet++; } int total = 2 * n - 1; HuffmanTree HT = (HuffmanTree)malloc(total * sizeof(HTNode)); if (!HT) { printf("HuffmanTree malloc faild!"); exit(-1); } int i; wet = wet - n; value = value - n; for (i = 0; i < n; i++) { HT[i].parent = -1; HT[i].lchild = -1; HT[i].rchild = -1; HT[i].weight = *wet; HT[i].value = *value; wet++; value++; } for (; i < total; i++) { HT[i].parent = -1; HT[i].lchild = -1; HT[i].rchild = -1; HT[i].weight = 0; } int min1, min2; for (i = n; i < total; i++) { select_minium(HT, i, min1, min2); HT[min1].parent = i; HT[min2].parent = i; HT[i].lchild = min1; HT[i].rchild = min2; HT[i].weight = HT[min1].weight + HT[min2].weight; } return HT; }
intcountWPL(HuffmanTree HT, int n) { int i, countRoads, WPL = 0; for (i = 0; i < n; i++) { int father = HT[i].parent; countRoads = 0;
while (father != -1) { countRoads++; father = HT[father].parent; } WPL += countRoads * HT[i].weight; } return WPL; }
voidselect_minium(HuffmanTree HT, int k, int &min1, int &min2) { min1 = min(HT, k); min2 = min(HT, k); }
intmin(HuffmanTree HT, int k) { int i = 0; int min; int min_weight; while (HT[i].parent != -1) i++; min_weight = HT[i].weight; min = i; for (; i < k; i++) { if (HT[i].weight < min_weight && HT[i].parent == -1) { min_weight = HT[i].weight; min = i; } } HT[min].parent = 1;
return min; }
voidHuffmanCoding(HuffmanTree HT, HuffmanCode &HC, int n) { HC = (HuffmanCode)malloc(n * sizeof(char *)); if (!HC) { printf("HuffmanCode request memory failed!"); exit(-1); } char *code = (char *)malloc(n * sizeof(char)); if (!code) { printf("code malloc faild!"); exit(-1); } code[n - 1] = '\0'; int i; for (i = 0; i < n; i++) { int current = i; int father = HT[i].parent; int start = n - 1; while (father != -1) { if (HT[father].lchild == current) code[--start] = '0'; else code[--start] = '1'; current = father; father = HT[father].parent; } HC[i] = (char *)malloc((n - start) * sizeof(char)); if (!HC[i]) { printf("HC[i] malloc faild!"); exit(-1); } strcpy(HC[i], code + start); } FILE *file = fopen("codefile.txt", "w"); if (NULL == file) { cout << "open error!\n"; } cout << "----------------------编码如下--------------------------" << endl; for (int i = 0; i < n; ++i) { printf("%c %s\n",HT[i].value,HC[i]); fprintf(file, "%c %s\n", HT[i].value, HC[i]); } fclose(file); free(code); cout << "已编码并写入文件codefile.txt" << endl; cout << "-----------------------------------------------------------" << endl; }
voidHuffmanDecoding(char ch[], HuffmanTree HT,int n){ int cnt = 0; int num[100]; HuffmanTree temp; for (int i = 0; i < strlen(ch); i++) { if (ch[i] == '0') { num[i] = 0; } else { num[i] = 1; } } FILE *file = fopen("textfile.txt", "w"); if (NULL == file) { cout << "open error!\n"; } cout << "译码结果为:"; fprintf(file, "%s", "译码结果:"); if (HT) { int cursor = 2*n- 2; while (cnt < strlen(ch)) { int temp = cursor; while ((HT[temp].lchild != -1) && (HT[temp].rchild != -1)) { if (num[cnt] == 0) { temp = HT[temp].lchild; } else { temp = HT[temp].rchild; } cnt++; } printf("%c", HT[temp].value); fprintf(file, "%c", HT[temp].value); } } cout << endl <<"已打印出译码结果并将结果存入textfile.txt中"<< endl; cout << "-------------------------------------------------------------" << endl; }