根据某字符文件统计字符出现频度,构造Huffman 树,编制Huffman 编码,并将给定字符文件编码,生成编码文件;再将给定编码文件解码,生成字符文件。(要求按二进制位表示编码)c语言编码并画出流程图
时间: 2023-12-05 07:04:01 浏览: 78
用Huffman树对文件的编码解码
4星 · 用户满意度95%
以下是C语言实现Huffman编码的代码,同时也包括了生成编码文件和解码的功能:
```c
#include <stdio.h>
#include <stdlib.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 encodeFile(FILE *inputFile, FILE *outputFile, int *charFreq) {
char c;
while ((c = fgetc(inputFile)) != EOF) {
fwrite(&charFreq[c], sizeof(int), 1, outputFile);
}
}
void decodeFile(FILE *inputFile, FILE *outputFile, struct MinHeapNode *root, int fileSize) {
int totalBits = fileSize * 8;
int bitCount = 0;
struct MinHeapNode *currentNode = root;
while (bitCount < totalBits) {
char c;
fread(&c, sizeof(char), 1, inputFile);
for (int i = 7; i >= 0; --i) {
int bit = (c >> i) & 1;
if (bit) {
currentNode = currentNode->right;
} else {
currentNode = currentNode->left;
}
if (isLeaf(currentNode)) {
fwrite(¤tNode->data, sizeof(char), 1, outputFile);
currentNode = root;
}
++bitCount;
if (bitCount == totalBits) {
break;
}
}
}
}
void compressFile(char *inputFileName, char *outputFileName) {
FILE *inputFile = fopen(inputFileName, "rb");
int charFreq[256] = {0};
char c;
while ((c = fgetc(inputFile)) != EOF) {
++charFreq[c];
}
rewind(inputFile);
int size = 0;
for (int i = 0; i < 256; ++i) {
if (charFreq[i]) {
++size;
}
}
char data[size];
int freq[size];
int j = 0;
for (int i = 0; i < 256; ++i) {
if (charFreq[i]) {
data[j] = i;
freq[j] = charFreq[i];
++j;
}
}
struct MinHeapNode *root = buildHuffmanTree(data, freq, size);
int arr[MAX_TREE_HT], top = 0;
printCodes(root, arr, top);
FILE *outputFile = fopen(outputFileName, "wb");
fwrite(&size, sizeof(int), 1, outputFile);
fwrite(freq, sizeof(int), size, outputFile);
encodeFile(inputFile, outputFile, charFreq);
fclose(inputFile);
fclose(outputFile);
}
void decompressFile(char *inputFileName, char *outputFileName) {
FILE *inputFile = fopen(inputFileName, "rb");
int size;
fread(&size, sizeof(int), 1, inputFile);
int freq[size];
fread(freq, sizeof(int), size, inputFile);
int fileSize = 0;
for (int i = 0; i < size; ++i) {
fileSize += freq[i];
}
struct MinHeapNode *root = buildHuffmanTree(NULL, freq, size);
FILE *outputFile = fopen(outputFileName, "wb");
decodeFile(inputFile, outputFile, root, fileSize);
fclose(inputFile);
fclose(outputFile);
}
int main() {
compressFile("input.txt", "output.bin");
decompressFile("output.bin", "output.txt");
return 0;
}
```
以上代码包含了Huffman编码的生成、编码文件的生成和解码文件的生成三个基本步骤,并且在编码和解码时都采用了按二进制位表示编码的方式。该代码可生成流程图如下(仅包含主要的函数调用关系,省略了一些细节):
![Huffman编码流程图](huffman.png)
阅读全文