C语言实现随机输入超过10字符及其频度,输出这些字符的哈夫曼编码
时间: 2023-06-18 22:07:22 浏览: 133
C语言实现哈夫曼编码
要实现这个功能,需要先进行以下几个步骤:
1. 统计每个字符出现的频度
2. 构建哈夫曼树
3. 生成哈夫曼编码
以下是 C 语言的实现代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_CHARACTERS 128 // ASCII 码表中的字符数
// 定义哈夫曼树节点结构体
typedef struct node {
char character;
int frequency;
struct node *left;
struct node *right;
} Node;
// 定义哈夫曼编码结构体
typedef struct code {
char character;
char *bits;
} Code;
// 统计字符频度
int *count_frequencies(char *str) {
int *frequencies = (int *) calloc(MAX_CHARACTERS, sizeof(int));
int length = strlen(str);
for (int i = 0; i < length; i++) {
frequencies[str[i]]++;
}
return frequencies;
}
// 创建哈夫曼树节点
Node *create_node(char character, int frequency) {
Node *node = (Node *) malloc(sizeof(Node));
node->character = character;
node->frequency = frequency;
node->left = NULL;
node->right = NULL;
return node;
}
// 建立哈夫曼树
Node *build_huffman_tree(int *frequencies) {
// 创建节点集合
Node **nodes = (Node **) malloc(MAX_CHARACTERS * sizeof(Node *));
int count = 0;
for (int i = 0; i < MAX_CHARACTERS; i++) {
if (frequencies[i] > 0) {
nodes[count++] = create_node(i, frequencies[i]);
}
}
while (count > 1) {
// 找到最小的两个节点
int min1 = 0, min2 = 1;
if (nodes[min1]->frequency > nodes[min2]->frequency) {
int temp = min1;
min1 = min2;
min2 = temp;
}
for (int i = 2; i < count; i++) {
if (nodes[i]->frequency < nodes[min1]->frequency) {
min2 = min1;
min1 = i;
} else if (nodes[i]->frequency < nodes[min2]->frequency) {
min2 = i;
}
}
// 创建新节点
Node *new_node = create_node('\0', nodes[min1]->frequency + nodes[min2]->frequency);
new_node->left = nodes[min1];
new_node->right = nodes[min2];
// 从节点集合中删除已处理的节点,添加新节点
nodes[min1] = new_node;
nodes[min2] = nodes[count - 1];
count--;
}
// 返回根节点
return nodes[0];
}
// 生成哈夫曼编码
void generate_huffman_codes(Node *root, char *prefix, int prefix_length, Code *codes) {
if (root->left == NULL && root->right == NULL) {
// 创建新编码
Code code = {root->character, (char *) malloc((prefix_length + 1) * sizeof(char))};
strncpy(code.bits, prefix, prefix_length);
code.bits[prefix_length] = '\0';
// 添加到编码表中
codes[root->character] = code;
} else {
// 处理左子树
prefix[prefix_length] = '0';
prefix_length++;
generate_huffman_codes(root->left, prefix, prefix_length, codes);
prefix_length--;
// 处理右子树
prefix[prefix_length] = '1';
prefix_length++;
generate_huffman_codes(root->right, prefix, prefix_length, codes);
prefix_length--;
}
}
// 主函数
int main() {
char input[100];
printf("请输入字符串:");
scanf("%s", input);
int *frequencies = count_frequencies(input);
Node *root = build_huffman_tree(frequencies);
Code codes[MAX_CHARACTERS];
char prefix[MAX_CHARACTERS];
generate_huffman_codes(root, prefix, 0, codes);
printf("字符\t频度\t哈夫曼编码\n");
for (int i = 0; i < MAX_CHARACTERS; i++) {
if (frequencies[i] > 0) {
printf("%c\t%d\t%s\n", i, frequencies[i], codes[i].bits);
}
}
// 释放内存
free(frequencies);
for (int i = 0; i < MAX_CHARACTERS; i++) {
if (codes[i].bits != NULL) {
free(codes[i].bits);
}
}
return 0;
}
```
这个程序会要求用户输入一个字符串,然后统计字符频度,建立哈夫曼树,最后生成哈夫曼编码并输出。
阅读全文