C语言实现随机输入超过10字符及其频度,输出这些字符的哈夫曼编码
时间: 2023-06-19 12:06:10 浏览: 69
实现此功能需要先构建哈夫曼树,再根据哈夫曼树生成对应的哈夫曼编码。以下是实现步骤:
1. 定义一个结构体存储字符及其出现频度
```c
struct char_freq {
char ch;
int freq;
};
```
2. 定义一个结构体存储哈夫曼树节点信息
```c
struct huffman_node {
char ch;
int freq;
int parent;
int left_child;
int right_child;
};
```
3. 定义一个函数用于统计字符出现频度
```c
void count_freq(char *str, struct char_freq *freq_arr, int *num_chars) {
int i, j;
int len = strlen(str);
struct char_freq temp;
for (i = 0; i < len; i++) {
for (j = 0; j < *num_chars; j++) {
if (freq_arr[j].ch == str[i]) {
freq_arr[j].freq++;
break;
}
}
if (j == *num_chars) {
temp.ch = str[i];
temp.freq = 1;
freq_arr[*num_chars] = temp;
(*num_chars)++;
}
}
}
```
4. 定义一个函数用于构建哈夫曼树
```c
void build_huffman_tree(struct huffman_node *tree, struct char_freq *freq_arr, int num_chars) {
int i, j;
int min1, min2;
for (i = 0; i < num_chars; i++) {
tree[i].ch = freq_arr[i].ch;
tree[i].freq = freq_arr[i].freq;
tree[i].parent = -1;
tree[i].left_child = -1;
tree[i].right_child = -1;
}
for (i = num_chars; i < 2 * num_chars - 1; i++) {
min1 = min2 = 0;
for (j = 0; j < i; j++) {
if (tree[j].parent == -1) {
if (tree[j].freq < tree[min1].freq) {
min2 = min1;
min1 = j;
} else if (tree[j].freq < tree[min2].freq) {
min2 = j;
}
}
}
tree[min1].parent = i;
tree[min2].parent = i;
tree[i].left_child = min1;
tree[i].right_child = min2;
tree[i].freq = tree[min1].freq + tree[min2].freq;
}
}
```
5. 定义一个函数用于生成哈夫曼编码
```c
void generate_huffman_code(struct huffman_node *tree, char **code_arr, int num_chars) {
int i, j, k;
char *code;
int parent, child;
for (i = 0; i < num_chars; i++) {
code = (char*) malloc(num_chars * sizeof(char));
k = 0;
child = i;
while (tree[child].parent != -1) {
parent = tree[child].parent;
if (tree[parent].left_child == child) {
code[k++] = '0';
} else if (tree[parent].right_child == child) {
code[k++] = '1';
}
child = parent;
}
code[k] = '\0';
for (j = 0; j < k / 2; j++) {
char temp = code[j];
code[j] = code[k - j - 1];
code[k - j - 1] = temp;
}
code_arr[i] = code;
}
}
```
6. 完整代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct char_freq {
char ch;
int freq;
};
struct huffman_node {
char ch;
int freq;
int parent;
int left_child;
int right_child;
};
void count_freq(char *str, struct char_freq *freq_arr, int *num_chars) {
int i, j;
int len = strlen(str);
struct char_freq temp;
for (i = 0; i < len; i++) {
for (j = 0; j < *num_chars; j++) {
if (freq_arr[j].ch == str[i]) {
freq_arr[j].freq++;
break;
}
}
if (j == *num_chars) {
temp.ch = str[i];
temp.freq = 1;
freq_arr[*num_chars] = temp;
(*num_chars)++;
}
}
}
void build_huffman_tree(struct huffman_node *tree, struct char_freq *freq_arr, int num_chars) {
int i, j;
int min1, min2;
for (i = 0; i < num_chars; i++) {
tree[i].ch = freq_arr[i].ch;
tree[i].freq = freq_arr[i].freq;
tree[i].parent = -1;
tree[i].left_child = -1;
tree[i].right_child = -1;
}
for (i = num_chars; i < 2 * num_chars - 1; i++) {
min1 = min2 = 0;
for (j = 0; j < i; j++) {
if (tree[j].parent == -1) {
if (tree[j].freq < tree[min1].freq) {
min2 = min1;
min1 = j;
} else if (tree[j].freq < tree[min2].freq) {
min2 = j;
}
}
}
tree[min1].parent = i;
tree[min2].parent = i;
tree[i].left_child = min1;
tree[i].right_child = min2;
tree[i].freq = tree[min1].freq + tree[min2].freq;
}
}
void generate_huffman_code(struct huffman_node *tree, char **code_arr, int num_chars) {
int i, j, k;
char *code;
int parent, child;
for (i = 0; i < num_chars; i++) {
code = (char*) malloc(num_chars * sizeof(char));
k = 0;
child = i;
while (tree[child].parent != -1) {
parent = tree[child].parent;
if (tree[parent].left_child == child) {
code[k++] = '0';
} else if (tree[parent].right_child == child) {
code[k++] = '1';
}
child = parent;
}
code[k] = '\0';
for (j = 0; j < k / 2; j++) {
char temp = code[j];
code[j] = code[k - j - 1];
code[k - j - 1] = temp;
}
code_arr[i] = code;
}
}
int main() {
char str[100];
struct char_freq freq_arr[100];
int num_chars = 0;
struct huffman_node tree[200];
char *code_arr[100];
int i, j;
printf("请输入一个字符串:");
scanf("%s", str);
count_freq(str, freq_arr, &num_chars);
build_huffman_tree(tree, freq_arr, num_chars);
generate_huffman_code(tree, code_arr, num_chars);
printf("字符\t频度\t编码\n");
for (i = 0; i < num_chars; i++) {
printf("%c\t%d\t%s\n", freq_arr[i].ch, freq_arr[i].freq, code_arr[i]);
}
return 0;
}
```
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![dsw](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)