#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXNUM 255
int num[26] = {0};
char saveLetter[26] = {'0'};
int countNum[26] = {0};
char codeTemp[MAXNUM] = {'0'};
typedef struct HTNode {
int weight; // 结点的权值
int parent, lchild, rchild; // 结点的双亲、左孩子、右孩子的下标
char data;
} HTNode, *HuffmanTree;
int Frequent(char s[], int *n)
{
int len = strlen(s);
int num[26] = {0};
memset(num, 0, sizeof(num)); // 全置为0
int i = 0, j = 0;//i用于定位字符,j是字母的相应下标,num数组计数
while (s[i] != '\0')
{
j = s[i] - 97;
num[j]++;
i++;
}
j = 0;
for (i = 0; i < 26; i++) {
if (num[i] != 0) {
saveLetter[j] = (char)(i + 97);//顺序存字母
countNum[j] = num[i];//存对应的次数
j++;
}
}//记录存在的字母
*n = j;//记录不同字母的数
for (int k = 0; k < *n; k++) {
if (k == *n - 1) {
printf("%c:%d\n", saveLetter[k], countNum[k]);
} else {
printf("%c:%d ", saveLetter[k], countNum[k]);
}
}
return *n;
}
void Select(HuffmanTree HT, int Max, int *s1, int *s2) {
int min = MAXNUM; // 最小值
int lmin = MAXNUM; // 次小值
for (int i = 1; i <= Max; i++) {
if (HT[i].parent == 0) {
if (HT[i].weight < min) {
min = HT[i].weight;
*s1 = i;
}
}
}
for (int i = 1; i <= Max; i++) {
if (HT[i].parent == 0) {
if (HT[i].weight < lmin && i != *s1) {
lmin = HT[i].weight;
*s2 = i;
}
}
}
}//找最小值下标
void CreateHuffmanTree(HuffmanTree *HT, int n) { // 构造哈夫曼树HT
if (n <= 1)
return;
int m = 2 * n - 1;
*HT = (HTNode *)malloc((m + 1) * sizeof(HTNode)); // 动态分配存储空间
if (!(*HT)) {
// 处理内存分配失败的情况
printf("Memory allocation failed.\n");
return;
}
for (int i = 1; i <= m; i++) {
(*HT)[i].parent = 0;
(*HT)[i].lchild = 0;
(*HT)[i].rchild = 0;
(*HT)[i].data = saveLetter[i - 1]; // 录入单个字母并创建空结点
}
for (int i = 1; i <= n; i++) {
(*HT)[i].weight = countNum[i - 1]; // 输入前n个单元中叶子结点的权值
}
int s1 = 0, s2 = 0;
// 初始化结束,下面开始创建哈夫曼树
for (int i = n + 1; i <= m; i++)
{//i从n+1开始的,用于储存生成的结点
Select(*HT, i - 1, &s1, &s2);
// 在HT[k](1<=k<=i-1)中选择两个其双亲域为0且权值最小的结点,并返回它们在HT中的序号s1和s2
(*HT)[s1].parent = i;
(*HT)[s2].parent = i;
// 得到新结点i,从森林中删除s1,s2,将s1和s2的双亲域由0改为i
(*HT)[i].lchild = s1;
(*HT)[i].rchild = s2; // s1,s2分别作为i的左右孩子
(*HT)[i].weight = (*HT)[s1].weight + (*HT)[s2].weight; // i的权值为左右孩子权值之和
if (i == m)
{
(*HT)[i].parent = 0;
}
}
}
void ShowHT(HuffmanTree HT, int n) {
int m = 2 * n - 1;
for (int i = 1; i <= m; i++) {
printf("%d ", i);
printf("%d ", HT[i].weight);
printf("%d ", HT[i].parent);
printf("%d ", HT[i].lchild);
printf("%d\n", HT[i].rchild);
}
}
typedef char** HuffmanTreeCode;//二维数组来存放字符串组
void CreateHuffmanCode(HuffmanTree HT, HuffmanTreeCode *HC, int n) {
// 从叶子到根逆向求每个字符的哈夫曼编码,存储在编码表HC中
*HC = (char **)malloc((n + 1) * sizeof(char *)); // 分配存储n个字符编码的编码表空间
char *cd;
cd = (char *)malloc(n * sizeof(char)); // 分配临时存放每个字符编码的动态数组空间
cd[n - 1] = '\0'; // 编码结束符
int start = 0, c, f = 0;
for (int i = 1; i <= n; i++) { // 逐个字符求哈夫曼编码
start = n - 1; // start开始时指向最后,即编码结束符位置
c = i;//c用于回溯
f = HT[i].parent; // f指向结点c的双亲结点
while (f != 0) { // 从叶子结点开始向上回溯,直到根结点
start--; // 回溯一次start向前指向一个位置
if (HT[f].lchild == c)
cd[start] = '0'; // 结点c是f的左孩子,则生成代码0
else
cd[start] = '1'; // 结点c是f的右孩子,则生成代码1
c = f; f = HT[f].parent; // 继续向上回溯
}
(*HC)[i] = (char *)malloc((n - start) * sizeof(char)); // 为第i个字符编码分配空间
strcpy((*HC)[i], &cd[start]); // 将求得的编码从临时空间cd复制到HC的当前行中
}
free(cd); // 释放临时空间
}
void ShowCode(HuffmanTree HT, HuffmanTreeCode HC, int n, char str1[]) {
for (int i = 0; str1[i] != '\0'; i++) {
for (int j = 1; j <= n; j++) {
if (str1[i] == saveLetter[j - 1]) {
printf("%s", HC[j]);
strcat(codeTemp, HC[j]);
}
}
}
printf("\n");
}
void CreateHuffmanReCode(HuffmanTree HT, HuffmanTreeCode HC, int n) {
int i = 2 * n - 1;
int j = 0;
while (codeTemp[j] != '\0') { // codeTemp是暂存01编码串
if (codeTemp[j] == '0') {
i = HT[i].lchild; // 左孩子
} else if (codeTemp[j] == '1') {
i = HT[i].rchild; // 右孩子
}
if (HT[i].lchild == 0) {
printf("%c", HT[i].data);
i = 2 * n - 1;
}
j++;
}
printf("\n");
}
void ShowHC(HuffmanTreeCode HC, int n) {
for (int i = 1; i <= n; i++) {
if (i != n)
printf("%c:%s ", saveLetter[i - 1], HC[i]);
else
printf("%c:%s\n", saveLetter[i - 1], HC[i]);
}
}
int main() {
int n;
char string[50] = { '0' };
while (1) {
scanf("%s", string);
if (string[0] == '0')
break;
n = Frequent(string, &n);
HuffmanTree ht;
HuffmanTreeCode hc;
CreateHuffmanTree(&ht,n);
CreateHuffmanCode(ht, &(hc), n);
ShowHT(ht, n);
ShowHC(hc, n);
ShowCode(ht, hc, n, string);
CreateHuffmanReCode(ht, hc, n);
memset(codeTemp, 0, sizeof codeTemp); // 清空数组
}
return 0;
}
02-12
6万+
05-27
1万+
12-04
11-22
1万+