在c语言中用Huffman编码方法,实现对任意通信字符的编码和解码。(输入任意字符串)
时间: 2024-05-12 13:13:09 浏览: 116
Huffman编码和解码的C语言实现
4星 · 用户满意度95%
好的,下面是用C语言实现Huffman编码和解码的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_CHAR_NUM 256 // 最大字符数
// 哈夫曼树节点结构体
typedef struct HuffmanNode {
char data; // 字符
int weight; // 权值
int parent; // 父节点下标
int lchild; // 左子节点下标
int rchild; // 右子节点下标
} HuffmanNode;
// 哈夫曼编码结构体
typedef struct HuffmanCode {
char data; // 字符
char *code; // 编码
} HuffmanCode;
// 选择最小的两个节点
void select(HuffmanNode *nodes, int n, int *min1, int *min2) {
int i;
for (i = 0; i < n; i++) {
if (nodes[i].parent == -1) {
*min1 = i;
break;
}
}
for (i = 0; i < n; i++) {
if (nodes[i].parent == -1 && nodes[i].weight < nodes[*min1].weight) {
*min1 = i;
}
}
for (i = 0; i < n; i++) {
if (nodes[i].parent == -1 && i != *min1) {
*min2 = i;
break;
}
}
for (i = 0; i < n; i++) {
if (nodes[i].parent == -1 && i != *min1 && nodes[i].weight < nodes[*min2].weight) {
*min2 = i;
}
}
}
// 构造哈夫曼树
void buildHuffmanTree(HuffmanNode *nodes, int n) {
int i, min1, min2;
for (i = 0; i < n - 1; i++) {
select(nodes, i + 1, &min1, &min2);
nodes[min1].parent = i + n;
nodes[min2].parent = i + n;
nodes[i + n].lchild = min1;
nodes[i + n].rchild = min2;
nodes[i + n].weight = nodes[min1].weight + nodes[min2].weight;
}
}
// 生成哈夫曼编码
void generateHuffmanCode(HuffmanNode *nodes, HuffmanCode *codes, int n) {
int i, j, parent, len;
char *code;
for (i = 0; i < n; i++) {
code = (char *)malloc(n * sizeof(char));
code[n - 1] = '\0';
parent = nodes[i].parent;
j = n - 2;
while (parent != -1) {
if (nodes[parent].lchild == i) {
code[j] = '0';
} else {
code[j] = '1';
}
i = parent;
parent = nodes[i].parent;
j--;
}
codes[i].data = nodes[i].data;
len = n - j - 2;
codes[i].code = (char *)malloc((len + 1) * sizeof(char));
strcpy(codes[i].code, &code[j + 1]);
free(code);
}
}
// 哈夫曼编码
void huffmanEncode(char *str, HuffmanCode *codes, char *result) {
int i, j, k;
for (i = 0; str[i] != '\0'; i++) {
for (j = 0; codes[j].data != str[i]; j++);
for (k = 0; codes[j].code[k] != '\0'; k++) {
*result++ = codes[j].code[k];
}
}
*result = '\0';
}
// 哈夫曼解码
void huffmanDecode(char *code, HuffmanNode *nodes, int n, char *result) {
int i, p = 2 * n - 2;
for (i = 0; code[i] != '\0'; i++) {
if (code[i] == '0') {
p = nodes[p].lchild;
} else {
p = nodes[p].rchild;
}
if (nodes[p].lchild == -1 && nodes[p].rchild == -1) {
*result++ = nodes[p].data;
p = 2 * n - 2;
}
}
*result = '\0';
}
int main() {
char str[100], result[100], code[100];
int i, n = 0;
HuffmanNode nodes[MAX_CHAR_NUM * 2 - 1] = {0};
HuffmanCode codes[MAX_CHAR_NUM] = {0};
printf("请输入待编码的字符串:");
scanf("%s", str);
// 统计字符出现频率
for (i = 0; str[i] != '\0'; i++) {
nodes[str[i]].data = str[i];
nodes[str[i]].weight++;
}
n = i;
// 初始化哈夫曼树
for (i = 0; i < MAX_CHAR_NUM * 2 - 1; i++) {
nodes[i].parent = -1;
nodes[i].lchild = -1;
nodes[i].rchild = -1;
}
// 构造哈夫曼树
buildHuffmanTree(nodes, n);
// 生成哈夫曼编码
generateHuffmanCode(nodes, codes, n);
// 哈夫曼编码
huffmanEncode(str, codes, result);
printf("编码结果:%s\n", result);
// 哈夫曼解码
huffmanDecode(result, nodes, n, code);
printf("解码结果:%s\n", code);
return 0;
}
```
这份代码实现了Huffman编码和解码功能,可以编码和解码任意字符串。
阅读全文