用c语言写哈夫曼树的文件压缩
时间: 2023-10-14 22:06:39 浏览: 142
hafuman.rar_hafuman_哈夫曼 文件 压缩_哈夫曼报文_哈夫曼树很易_字符 哈夫曼编码
5星 · 资源好评率100%
下面是使用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 *temp = (struct MinHeapNode*)malloc(sizeof(struct MinHeapNode));
temp->left = temp->right = NULL;
temp->data = data;
temp->freq = freq;
return temp;
}
// 创建一个空的哈夫曼树
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 isSizeOne(struct MinHeap* minHeap) {
return (minHeap->size == 1);
}
// 从最小堆中取出最小的结点
struct MinHeapNode* extractMin(struct MinHeap* minHeap) {
struct MinHeapNode* temp = minHeap->array[0];
minHeap->array[0] = minHeap->array[minHeap->size - 1];
--minHeap->size;
minHeapify(minHeap, 0);
return temp;
}
// 插入一个结点到最小堆中
void insertMinHeap(struct MinHeap* minHeap, struct MinHeapNode* minHeapNode) {
++minHeap->size;
int i = minHeap->size - 1;
while (i && minHeapNode->freq < minHeap->array[(i - 1) / 2]->freq) {
minHeap->array[i] = minHeap->array[(i - 1) / 2];
i = (i - 1) / 2;
}
minHeap->array[i] = minHeapNode;
}
// 判断是否为叶子结点
int isLeaf(struct MinHeapNode* root) {
return !(root->left) && !(root->right);
}
// 创建一个最小堆并构建哈夫曼树
struct MinHeap* createAndBuildMinHeap(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;
while (!isSizeOne(minHeap)) {
struct MinHeapNode *left = extractMin(minHeap);
struct MinHeapNode *right = extractMin(minHeap);
struct MinHeapNode *top = newNode('$', left->freq + right->freq);
top->left = left;
top->right = right;
insertMinHeap(minHeap, top);
}
return extractMin(minHeap);
}
// 打印哈夫曼编码
void printCodes(struct MinHeapNode* root, int arr[], int top, FILE *fp) {
// 遍历到叶子结点
if (root->left) {
arr[top] = 0;
printCodes(root->left, arr, top + 1, fp);
}
if (root->right) {
arr[top] = 1;
printCodes(root->right, arr, top + 1, fp);
}
if (isLeaf(root)) {
fprintf(fp, "%c:", root->data);
for (int i = 0; i < top; ++i) {
fprintf(fp, "%d", arr[i]);
}
fprintf(fp, "\n");
}
}
// 压缩文件
void compressFile(char* fileName, FILE *fp) {
int freq[256] = {0};
char data[256];
// 统计字符出现频率
int ch;
while ((ch = fgetc(fp)) != EOF) {
++freq[ch];
}
int size = 0;
for (int i = 0; i < 256; ++i) {
if (freq[i] != 0) {
data[size++] = (char)i;
}
}
// 构建哈夫曼树
struct MinHeapNode* root = createAndBuildMinHeap(data, freq, size);
int arr[MAX_TREE_HT], top = 0;
printCodes(root, arr, top, stdout);
}
int main() {
char fileName[] = "test.txt";
FILE *fp = fopen(fileName, "r");
if (fp == NULL) {
printf("Error opening file\n");
exit(1);
}
compressFile(fileName, fp);
fclose(fp);
return 0;
}
```
这段代码实现了从文件中读取数据,统计字符出现频率,构建哈夫曼树,生成哈夫曼编码,以及输出哈夫曼编码。这个示例仅仅是生成了哈夫曼编码,还需要将原始文本进行编码压缩,这个可以参考哈夫曼编码的实现来完成。
阅读全文