设计实现一个基于哈夫曼算法的压缩软件,统计文件中各字符出现的频次,设计哈夫曼编码,用c语言写出并写出详细注释,要写出完整代码
时间: 2024-05-16 15:12:58 浏览: 177
基于哈夫曼编码的文件压缩解压程序的C语言实现
好的,以下是基于哈夫曼算法的压缩软件的实现。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_TREE_HT 1000
// 定义哈夫曼节点
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 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;
}
// 建立哈夫曼树
struct MinHeapNode* buildHuffmanTree(char data[], int freq[], int size) {
struct MinHeapNode *left, *right, *top;
struct MinHeap* minHeap = createMinHeap(size);
for (int i = 0; i < size; ++i) {
insertMinHeap(minHeap, newNode(data[i], freq[i]));
}
while (!isSizeOne(minHeap)) {
left = extractMin(minHeap);
right = extractMin(minHeap);
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) {
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 (!root->left && !root->right) {
printf("%c: ", root->data);
for (int i = 0; i < top; ++i) {
printf("%d", arr[i]);
}
printf("\n");
}
}
// 压缩函数
void compressFile(char* fileName) {
FILE* fp = fopen(fileName, "rb");
if (fp == NULL) {
printf("File not found!\n");
return;
}
fseek(fp, 0, SEEK_END);
int fileSize = ftell(fp);
rewind(fp);
// 统计文件中各字符出现的频次
int freq[256] = {0};
char c;
for (int i = 0; i < fileSize; ++i) {
fread(&c, sizeof(char), 1, fp);
freq[c]++;
}
// 创建哈夫曼树
char data[256];
int j = 0;
for (int i = 0; i < 256; ++i) {
if (freq[i] != 0) {
data[j++] = (char)i;
}
}
struct MinHeapNode* root = buildHuffmanTree(data, freq, j);
// 打印哈夫曼编码
int arr[MAX_TREE_HT], top = 0;
printCodes(root, arr, top);
fclose(fp);
}
// 主函数
int main(int argc, char const *argv[]) {
if (argc != 2) {
printf("Usage: %s <file_name>\n", argv[0]);
return 1;
}
compressFile((char*)argv[1]);
return 0;
}
```
以上代码实现了哈夫曼树的构建以及哈夫曼编码的生成,但是并没有实现具体的压缩和解压缩功能。如果需要实现完整的压缩软件,还需要编写相应的压缩和解压缩算法。
阅读全文