对一个给定的文本文件,对其进行哈夫曼编码,并计算压缩率。C语言代码
时间: 2024-05-07 13:23:06 浏览: 156
以下是一个简单的C语言代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_TREE_HT 100
// 结点结构体
struct MinHeapNode {
char data;
unsigned freq;
struct MinHeapNode *left, *right;
};
// 最小堆结构体
struct MinHeap {
unsigned size;
unsigned capacity;
struct MinHeapNode** array;
};
// 创建新的结点
struct MinHeapNode* newNode(char data, unsigned freq) {
struct MinHeapNode* node = (struct MinHeapNode*) malloc(sizeof(struct MinHeapNode));
node->left = node->right = NULL;
node->data = data;
node->freq = freq;
return node;
}
// 创建最小堆
struct MinHeap* createMinHeap(unsigned capacity) {
struct MinHeap* minHeap = (struct MinHeap*) malloc(sizeof(struct MinHeap));
minHeap->size = 0;
minHeap->capacity = capacity;
minHeap->array = (struct MinHeapNode**) malloc(minHeap->capacity * sizeof(struct MinHeapNode*));
return minHeap;
}
// 交换两个结点
void swapMinHeapNode(struct MinHeapNode** a, struct MinHeapNode** b) {
struct MinHeapNode* t = *a;
*a = *b;
*b = t;
}
// 最小堆的堆化
void minHeapify(struct MinHeap* minHeap, int idx) {
int smallest = idx;
int left = 2 * idx + 1;
int right = 2 * idx + 2;
if (left < minHeap->size && minHeap->array[left]->freq < minHeap->array[smallest]->freq)
smallest = left;
if (right < minHeap->size && minHeap->array[right]->freq < minHeap->array[smallest]->freq)
smallest = right;
if (smallest != idx) {
swapMinHeapNode(&minHeap->array[smallest], &minHeap->array[idx]);
minHeapify(minHeap, smallest);
}
}
// 判断最小堆是否为叶子结点
int isLeaf(struct MinHeapNode* root) {
return !(root->left) && !(root->right);
}
// 创建最小堆,并插入结点
struct MinHeap* buildMinHeap(char data[], int freq[], int size) {
struct MinHeap* minHeap = createMinHeap(size);
for (int i = 0; i < size; ++i)
minHeap->array[i] = newNode(data[i], freq[i]);
minHeap->size = size;
for (int i = (minHeap->size - 1) / 2; i >= 0; --i)
minHeapify(minHeap, i);
return minHeap;
}
// 构建哈夫曼树
struct MinHeapNode* buildHuffmanTree(char data[], int freq[], int size) {
struct MinHeapNode *left, *right, *top;
struct MinHeap* minHeap = buildMinHeap(data, freq, size);
while (minHeap->size != 1) {
left = minHeap->array[0];
right = minHeap->array[1];
top = newNode('$', left->freq + right->freq);
top->left = left;
top->right = right;
minHeap->array[0] = top;
minHeap->size--;
minHeapify(minHeap, 0);
}
return minHeap->array[0];
}
// 打印哈夫曼编码
void printCodes(struct MinHeapNode* root, int arr[], int top) {
if (root->left) {
arr[top] = 0;
printCodes(root->left, arr, top + 1);
}
if (root->right) {
arr[top] = 1;
printCodes(root->right, arr, top + 1);
}
if (isLeaf(root)) {
printf("%c: ", root->data);
for (int i = 0; i < top; ++i)
printf("%d", arr[i]);
printf("\n");
}
}
// 进行哈夫曼编码
void HuffmanCodes(char data[], int freq[], int size) {
struct MinHeapNode* root = buildHuffmanTree(data, freq, size);
int arr[MAX_TREE_HT], top = 0;
printCodes(root, arr, top);
}
// 计算压缩率
float compressionRatio(int uncompressed_size, int compressed_size) {
return (float)(uncompressed_size - compressed_size) / uncompressed_size;
}
// 主函数
int main() {
char data[] = {'a', 'b', 'c', 'd', 'e', 'f'};
int freq[] = {5, 9, 12, 13, 16, 45};
int size = sizeof(data) / sizeof(data[0]);
HuffmanCodes(data, freq, size);
// 假设原始文件大小为100字节,压缩后文件大小为60字节
int uncompressed_size = 100;
int compressed_size = 60;
float ratio = compressionRatio(uncompressed_size, compressed_size);
printf("Compression ratio: %.2f%%\n", ratio * 100);
return 0;
}
```
以上代码是一个简单的哈夫曼编码实现,其中 `data` 数组存储字符,`freq` 数组存储对应字符出现的频率,`size` 表示数组的长度。通过 `HuffmanCodes` 函数进行哈夫曼编码并打印出每个字符的编码。最后通过 `compressionRatio` 函数计算压缩率。
阅读全文