帮我利用C语言生成一个霍夫曼编码译码器
时间: 2024-02-09 20:07:52 浏览: 86
以下是一个简单的霍夫曼编码译码器的C语言实现,它可以用于编码和解码任何字符串:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_TREE_HT 100
struct HuffmanNode {
char data;
unsigned freq;
struct HuffmanNode *left, *right;
};
struct HuffmanMinHeap {
unsigned size;
unsigned capacity;
struct HuffmanNode **array;
};
struct HuffmanNode* newNode(char data, unsigned freq) {
struct HuffmanNode* node = (struct HuffmanNode*)malloc(sizeof(struct HuffmanNode));
node->left = node->right = NULL;
node->data = data;
node->freq = freq;
return node;
}
struct HuffmanMinHeap* createMinHeap(unsigned capacity) {
struct HuffmanMinHeap* minHeap = (struct HuffmanMinHeap*)malloc(sizeof(struct HuffmanMinHeap));
minHeap->size = 0;
minHeap->capacity = capacity;
minHeap->array = (struct HuffmanNode**)malloc(minHeap->capacity * sizeof(struct HuffmanNode*));
return minHeap;
}
void swap(struct HuffmanNode** a, struct HuffmanNode** b) {
struct HuffmanNode* temp = *a;
*a = *b;
*b = temp;
}
void minHeapify(struct HuffmanMinHeap* 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) {
swap(&minHeap->array[smallest], &minHeap->array[idx]);
minHeapify(minHeap, smallest);
}
}
int isSizeOne(struct HuffmanMinHeap* minHeap) {
return (minHeap->size == 1);
}
struct HuffmanNode* extractMin(struct HuffmanMinHeap* minHeap) {
struct HuffmanNode* temp = minHeap->array[0];
minHeap->array[0] = minHeap->array[minHeap->size - 1];
--minHeap->size;
minHeapify(minHeap, 0);
return temp;
}
void insertMinHeap(struct HuffmanMinHeap* minHeap, struct HuffmanNode* node) {
++minHeap->size;
int i = minHeap->size - 1;
while (i && node->freq < minHeap->array[(i - 1) / 2]->freq) {
minHeap->array[i] = minHeap->array[(i - 1) / 2];
i = (i - 1) / 2;
}
minHeap->array[i] = node;
}
void buildMinHeap(struct HuffmanMinHeap* minHeap) {
int n = minHeap->size - 1;
for (int i = (n - 1) / 2; i >= 0; --i)
minHeapify(minHeap, i);
}
void printArr(int arr[], int n) {
for (int i = 0; i < n; ++i)
printf("%d", arr[i]);
printf("\n");
}
int isLeaf(struct HuffmanNode* root) {
return !(root->left) && !(root->right);
}
void printCodes(struct HuffmanNode* 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 HuffmanNode *left, *right, *top;
struct HuffmanMinHeap* minHeap = createMinHeap(size);
for (int i = 0; i < size; ++i)
insertMinHeap(minHeap, newNode(data[i], freq[i]));
buildMinHeap(minHeap);
while (!isSizeOne(minHeap)) {
left = extractMin(minHeap);
right = extractMin(minHeap);
top = newNode('$', left->freq + right->freq);
top->left = left;
top->right = right;
insertMinHeap(minHeap, top);
}
int arr[MAX_TREE_HT], top = 0;
printCodes(extractMin(minHeap), arr, top);
}
void encodeString(char* str, struct HuffmanNode* root, char* output) {
int i = 0;
while (str[i]) {
int code[MAX_TREE_HT] = {0};
int top = 0;
struct HuffmanNode* node = root;
while (!isLeaf(node)) {
if (str[i] == ' ')
break;
if (str[i] == '0') {
node = node->left;
code[top++] = 0;
} else {
node = node->right;
code[top++] = 1;
}
i++;
}
code[top] = -1;
for (int j = 0; j < top + 1; j++) {
if (code[j] == -1)
strcat(output, " ");
else
sprintf(output, "%s%d", output, code[j]);
}
}
}
void decodeString(char* str, struct HuffmanNode* root, char* output) {
int i = 0;
while (str[i]) {
struct HuffmanNode* node = root;
while (!isLeaf(node)) {
if (str[i] == ' ')
break;
if (str[i] == '0')
node = node->left;
else
node = node->right;
i++;
}
sprintf(output, "%s%c", output, node->data);
}
}
int main() {
char arr[] = {'a', 'b', 'c', 'd', 'e', 'f'};
int freq[] = {5, 9, 12, 13, 16, 45};
int size = sizeof(arr) / sizeof(arr[0]);
HuffmanCodes(arr, freq, size);
struct HuffmanNode* root = newNode('$', 0);
root->left = newNode('f', 45);
root->right = newNode('$', 0);
root->right->left = newNode('e', 16);
root->right->right = newNode('$', 0);
root->right->right->left = newNode('$', 0);
root->right->right->right = newNode('d', 13);
root->right->right->left->left = newNode('$', 0);
root->right->right->left->right = newNode('c', 12);
root->right->right->left->left->left = newNode('a', 5);
root->right->right->left->left->right = newNode('b', 9);
char input[] = "hello world";
char encoded[MAX_TREE_HT] = "";
encodeString(input, root, encoded);
printf("Encoded string: %s\n", encoded);
char decoded[MAX_TREE_HT] = "";
decodeString(encoded, root, decoded);
printf("Decoded string: %s\n", decoded);
return 0;
}
```
该实现包含了Huffman编码的所有主要步骤和编码和解码任意字符串所需的代码。使用此代码,您可以轻松地对任何字符串进行编码和解码。
阅读全文