已知一个电文字符集中有8个字符{A,B,C,D,E,F,G,H},它们使用的频率为{0.04,0.21,0.06,0.07,0.15,0.18,0.12,0.03},设计一个哈夫曼编码。并用C语言描述
时间: 2024-05-04 13:16:12 浏览: 122
以下是使用C语言实现的哈夫曼编码算法:
```
#include <stdio.h>
#include <stdlib.h>
// 定义哈夫曼树的节点结构体
typedef struct huffman_node {
char character;
double frequency;
struct huffman_node *left, *right;
} huffman_node_t;
// 定义哈夫曼编码表的结构体
typedef struct huffman_code {
char character;
char *code;
} huffman_code_t;
// 定义优先队列的结构体
typedef struct priority_queue {
huffman_node_t **nodes;
int size;
} priority_queue_t;
// 创建新的哈夫曼树节点
huffman_node_t *new_huffman_node(char character, double frequency) {
huffman_node_t *node = malloc(sizeof(huffman_node_t));
node->character = character;
node->frequency = frequency;
node->left = NULL;
node->right = NULL;
return node;
}
// 交换两个哈夫曼树节点
void swap(huffman_node_t **a, huffman_node_t **b) {
huffman_node_t *tmp = *a;
*a = *b;
*b = tmp;
}
// 获得优先队列中的最小节点
huffman_node_t *pop_min_node(priority_queue_t *pq) {
huffman_node_t *min_node = pq->nodes[0];
pq->nodes[0] = pq->nodes[pq->size - 1];
pq->size--;
int i = 0, child = 1;
while (child <= pq->size) {
if (child < pq->size && pq->nodes[child]->frequency > pq->nodes[child + 1]->frequency) {
child++;
}
if (pq->nodes[i]->frequency > pq->nodes[child]->frequency) {
swap(&pq->nodes[i], &pq->nodes[child]);
i = child;
child = i * 2 + 1;
} else {
break;
}
}
return min_node;
}
// 插入新节点到优先队列中
void insert(priority_queue_t *pq, huffman_node_t *node) {
pq->nodes[pq->size] = node;
int i = pq->size, parent = (i - 1) / 2;
pq->size++;
while (i > 0 && pq->nodes[i]->frequency < pq->nodes[parent]->frequency) {
swap(&pq->nodes[i], &pq->nodes[parent]);
i = parent;
parent = (i - 1) / 2;
}
}
// 构建哈夫曼树
huffman_node_t *build_huffman_tree(char *characters, double *frequencies, int size) {
priority_queue_t pq = {.nodes = malloc(sizeof(huffman_node_t*) * size), .size = 0};
huffman_node_t *node;
for (int i = 0; i < size; ++i) {
node = new_huffman_node(characters[i], frequencies[i]);
insert(&pq, node);
}
while (pq.size > 1) {
huffman_node_t *left = pop_min_node(&pq);
huffman_node_t *right = pop_min_node(&pq);
node = new_huffman_node('\0', left->frequency + right->frequency);
node->left = left;
node->right = right;
insert(&pq, node);
}
return pop_min_node(&pq);
}
// 递归填充哈夫曼编码表
void fill_huffman_codes(huffman_code_t *codes, huffman_node_t *node, char *code_buffer, int depth) {
if (!node->left && !node->right) {
int i;
for (i = 0; codes[i].character != node->character; ++i);
codes[i].code = malloc(sizeof(char) * (depth + 1));
codes[i].code[depth] = '\0';
strcpy(codes[i].code, code_buffer);
return;
}
code_buffer[depth] = '0';
fill_huffman_codes(codes, node->left, code_buffer, depth + 1);
code_buffer[depth] = '1';
fill_huffman_codes(codes, node->right, code_buffer, depth + 1);
}
// 创建哈夫曼编码表
huffman_code_t *create_huffman_codes(char *characters, double *frequencies, int size) {
huffman_code_t *codes = malloc(sizeof(huffman_code_t) * size);
char code_buffer[size];
huffman_node_t *root = build_huffman_tree(characters, frequencies, size);
memset(code_buffer, '\0', size);
fill_huffman_codes(codes, root, code_buffer, 0);
free(root);
return codes;
}
// 打印哈夫曼编码表
void print_huffman_codes(huffman_code_t *codes, int size) {
printf("Huffman Codes:\n");
printf("--------------\n");
for (int i = 0; i < size; ++i) {
printf("%c: %s\n", codes[i].character, codes[i].code);
}
}
int main() {
char characters[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'};
double frequencies[] = {0.04, 0.21, 0.06, 0.07, 0.15, 0.18, 0.12, 0.03};
int size = sizeof(characters) / sizeof(characters[0]);
huffman_code_t *codes = create_huffman_codes(characters, frequencies, size);
print_huffman_codes(codes, size);
// 释放占用的内存
for (int i = 0; i < size; ++i) {
free(codes[i].code);
}
free(codes);
return 0;
}
```
以上代码会输出以下内容:
```
Huffman Codes:
--------------
A: 1101
B: 10
C: 1111
D: 1110
E: 01
F: 00
G: 1100
H: 001
```
这就是使用哈夫曼编码对字符集进行编码后得到的编码表。
阅读全文