请帮我写一个使用c语言实现的数据结构顺序表哈夫曼编码译码器(详细代码并附注释)
时间: 2024-12-22 14:21:17 浏览: 23
哈夫曼编码是一种基于频率压缩数据的算法,主要用于文本编码,通过构建一棵最优二叉树来生成编码。以下是使用C语言实现的简单顺序表(数组)哈夫曼编码译码器示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义节点结构体
typedef struct Node {
char data;
int freq; // 频率
struct Node* left; // 左子树
struct Node* right; // 右子树
} Node;
// 创建新节点
Node* newNode(char data, int freq) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->freq = freq;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// 插入节点到堆中
Node* insertNode(Node** heap, char data, int freq) {
Node* newNode = newNode(data, freq);
if (*heap == NULL || (*heap)->freq > newNode->freq) { // 如果堆为空或新节点频率更低,则作为根节点
*heap = newNode;
} else {
Node* temp = *heap;
while (temp != NULL) {
if (newNode->freq < temp->freq) {
if (temp->left == NULL) {
temp->left = newNode;
break;
} else {
temp = temp->left;
}
} else {
if (temp->right == NULL) {
temp->right = newNode;
break;
} else {
temp = temp->right;
}
}
}
}
return *heap;
}
// 哈夫曼编码构建过程
void buildHuffmanTree(Node** heap, Node** huffTree, int size) {
if (size <= 1) {
*huffTree = *heap;
return;
}
Node* nodeA = *heap[0];
Node* nodeB = *heap[1];
delete *heap[0]; // 删除堆顶两个节点
delete *heap[1];
heap[0] = insertNode(heap, '\0', nodeA->freq + nodeB->freq); // 合并节点
buildHuffmanTree(heap, huffTree, size - 1); // 继续构建新的堆
buildHuffmanTree(&heap[0]->left, huffTree, size / 2); // 左子树
buildHuffmanTree(&heap[0]->right, &(*huffTree)->right, size / 2); // 右子树
}
// 生成哈夫曼编码
void generateCodes(Node* root, char codes[], int index, char code) {
if (root == NULL) return;
generateCodes(root->left, codes, index + 1, '0' + code);
generateCodes(root->right, codes, index + 1, '1' + code);
if (root->data != '\0') { // 根节点对应字符,添加编码
codes[root->data] = code;
}
}
// 解码函数
int decode(const char* input, const char* codes, char decoded[]) {
int i = 0, len = strlen(input), decodedIndex = 0;
for (i = 0; i < len; ++i) {
decoded[decodedIndex++] = codes[input[i]];
}
decoded[decodedIndex] = '\0'; // 添加结束符
return decodedIndex;
}
int main() {
char data[] = "hello world!";
int freqs[] = {5, 9, 2, 6, 1, 4, 8, 7, 3};
Node** heap = malloc(sizeof(Node*) * 10);
Node* huffTree = NULL;
int size = sizeof(freqs) / sizeof(freqs[0]);
// 构建哈夫曼树
for (int i = 0; i < size; ++i) {
insertNode(heap, data[i], freqs[i]);
}
buildHuffmanTree(heap, &huffTree, size);
// 生成哈夫曼编码
char huffmanCodes[256] = {0}; // 假设最多256种字符
generateCodes(huffTree, huffmanCodes, 0, '');
// 编码
char encodedData[strlen(data)];
for (int i = 0; i < strlen(data); ++i) {
encodedData[i] = huffmanCodes[data[i]];
}
// 解码
char decodedData[strlen(encodedData) + 1];
int decodedLength = decode(encodedData, huffmanCodes, decodedData);
printf("Original text: %s\n", data);
printf("Encoded text: %s\n", encodedData);
printf("Decoded text: %s\n", decodedData);
free(heap[0]);
free(heap);
free(huffTree);
return 0;
}
```
以上代码首先创建了一个哈夫曼树,然后生成了每个字符的编码,并实现了编码和解码功能。注意,这个版本仅处理ASCII字符,实际应用可能需要更复杂的方式来存储非ASCII字符。
阅读全文