哈夫曼编码压缩解压缩c语言代码
时间: 2023-10-12 07:05:13 浏览: 135
C语言实现哈夫曼编码压缩和解压各种文件
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;
}
void buildMinHeap(struct MinHeap* minHeap) {
int n = minHeap->size - 1;
int i;
for (i = (n - 1) / 2; i >= 0; --i)
minHeapify(minHeap, i);
}
void printArr(int arr[], int n) {
int i;
for (i = 0; i < n; ++i)
printf("%d", arr[i]);
printf("\n");
}
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;
buildMinHeap(minHeap);
return minHeap;
}
struct MinHeapNode* buildHuffmanTree(char data[], int freq[], int size) {
struct MinHeapNode *left, *right, *top;
struct MinHeap* minHeap = createAndBuildMinHeap(data, freq, size);
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 (isLeaf(root)) {
printf("%c: ", root->data);
printArr(arr, top);
}
}
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);
}
void compress(char* inputFilePath, char* outputFilePath) {
FILE* inputFile = fopen(inputFilePath, "rb");
if (!inputFile) {
printf("Error: Could not open file %s.\n", inputFilePath);
return;
}
fseek(inputFile, 0, SEEK_END);
unsigned long inputSize = ftell(inputFile);
fseek(inputFile, 0, SEEK_SET);
char* inputData = (char*)malloc(inputSize);
fread(inputData, 1, inputSize, inputFile);
fclose(inputFile);
int freq[256] = { 0 };
for (int i = 0; i < inputSize; ++i)
++freq[inputData[i]];
char data[256];
int size = 0;
for (int i = 0; i < 256; ++i) {
if (freq[i] != 0) {
data[size] = (char)i;
++size;
}
}
HuffmanCodes(data, freq, size);
FILE* outputFile = fopen(outputFilePath, "wb");
if (!outputFile) {
printf("Error: Could not create file %s.\n", outputFilePath);
return;
}
struct MinHeapNode* root = buildHuffmanTree(data, freq, size);
int arr[MAX_TREE_HT], top = 0;
printCodes(root, arr, top);
fwrite(&size, sizeof(size), 1, outputFile);
for (int i = 0; i < size; ++i) {
fwrite(&data[i], sizeof(data[i]), 1, outputFile);
fwrite(&freq[(unsigned char)data[i]], sizeof(freq[(unsigned char)data[i]]), 1, outputFile);
}
int bitIndex = 0;
char byte = 0;
for (int i = 0; i < inputSize; ++i) {
int arrIndex = 0;
struct MinHeapNode* node = root;
while (inputData[i] != node->data) {
if (inputData[i] < node->data)
node = node->left;
else
node = node->right;
arrIndex++;
}
for (int j = 0; j < arrIndex; ++j) {
byte |= ((arr[j] << (bitIndex % 8)) & (1 << (bitIndex % 8)));
++bitIndex;
if (bitIndex % 8 == 0) {
fwrite(&byte, sizeof(byte), 1, outputFile);
byte = 0;
}
}
}
if (bitIndex % 8 != 0)
fwrite(&byte, sizeof(byte), 1, outputFile);
fclose(outputFile);
printf("Compression complete. Original size: %lu bytes. Compressed size: %ld bytes.\n", inputSize, ftell(outputFile));
}
void decompress(char* inputFilePath, char* outputFilePath) {
FILE* inputFile = fopen(inputFilePath, "rb");
if (!inputFile) {
printf("Error: Could not open file %s.\n", inputFilePath);
return;
}
int size;
fread(&size, sizeof(size), 1, inputFile);
char data[256];
int freq[256];
for (int i = 0; i < size; ++i) {
fread(&data[i], sizeof(data[i]), 1, inputFile);
fread(&freq[(unsigned char)data[i]], sizeof(freq[(unsigned char)data[i]]), 1, inputFile);
}
struct MinHeapNode* root = buildHuffmanTree(data, freq, size);
FILE* outputFile = fopen(outputFilePath, "wb");
if (!outputFile) {
printf("Error: Could not create file %s.\n", outputFilePath);
return;
}
unsigned long outputSize = 0;
int bitIndex = 0;
char byte = 0;
struct MinHeapNode* node = root;
while (1) {
int bit = fgetc(inputFile);
if (bit == EOF)
break;
for (int i = 0; i < 8; ++i) {
if (bit & (1 << i))
node = node->right;
else
node = node->left;
if (isLeaf(node)) {
fwrite(&node->data, sizeof(node->data), 1, outputFile);
outputSize += sizeof(node->data);
node = root;
}
}
}
fclose(inputFile);
fclose(outputFile);
printf("Decompression complete. Original size: %d bytes. Decompressed size: %lu bytes.\n", size, outputSize);
}
int main() {
char inputFilePath[] = "input.txt";
char compressedFilePath[] = "compressed.bin";
char decompressedFilePath[] = "decompressed.txt";
compress(inputFilePath, compressedFilePath);
decompress(compressedFilePath, decompressedFilePath);
return 0;
}
```
该示例包括哈夫曼树的构建、编码、压缩和解码、解压缩等功能。其中,compress函数将输入文件进行压缩,并将压缩结果写入到输出文件中;decompress函数将压缩文件进行解压缩,并将解压缩结果写入到输出文件中。
阅读全文