求输入一串字符串,根据给定的字符串中字符出现的频率建立相应的哈夫曼树,构造哈夫曼编码表,在此基础上可以对压缩文件进行压缩(即编码),同时可以对压缩后的二进制编码文件进行解压(即译码)C语言代码
时间: 2024-05-01 15:19:57 浏览: 116
基于C++进行数据结构算法之实验(哈夫曼树)【100012523】
以下是一个简单的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 encode(char *str, char *output) {
char data[256];
int freq[256] = {0};
int i = 0;
while (str[i] != '\0') {
freq[(int)str[i]]++;
i++;
}
int size = 0;
for (i = 0; i < 256; i++) {
if (freq[i] != 0) {
data[size++] = (char)i;
}
}
HuffmanCodes(data, freq, size);
// create the Huffman code table
int codeTable[256][MAX_TREE_HT];
int codeTableSize[256] = {0};
int arr[MAX_TREE_HT], top = 0;
printCodes(root, arr, top);
for (i = 0; i < 256; i++) {
if (freq[i] != 0) {
for (int j = 0; j < top; j++) {
codeTable[i][j] = arr[j];
}
codeTableSize[i] = top;
}
}
// encode the string
i = 0;
int j = 0;
while (str[i] != '\0') {
int c = (int)str[i];
for (int k = 0; k < codeTableSize[c]; k++) {
output[j++] = codeTable[c][k] + '0';
}
i++;
}
output[j] = '\0';
}
void decode(char *input, char *output) {
int freq[256] = {0};
int i = 0;
while (input[i] != '\0') {
freq[input[i] - '0']++;
i++;
}
char data[256];
int size = 0;
for (i = 0; i < 256; i++) {
if (freq[i] != 0) {
data[size++] = (char)i;
}
}
struct MinHeapNode* root = buildHuffmanTree(data, freq, size);
// decode the string
struct MinHeapNode* curr = root;
i = 0;
int j = 0;
while (input[i] != '\0') {
if (input[i] == '0') {
curr = curr->left;
} else {
curr = curr->right;
}
if (isLeaf(curr)) {
output[j++] = curr->data;
curr = root;
}
i++;
}
output[j] = '\0';
}
int main() {
char str[] = "hello world";
char output[1000];
char decoded[1000];
// encode the string
encode(str, output);
printf("Encoded string: %s\n", output);
// decode the string
decode(output, decoded);
printf("Decoded string: %s\n", decoded);
return 0;
}
```
阅读全文