用c语言哈夫曼编码和译码
时间: 2024-05-07 16:11:53 浏览: 93
哈夫曼编码系统应用
以下是使用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;
}
// 判断节点是否是叶子节点
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;
for (int i = (minHeap->size - 1) / 2; i >= 0; --i)
minHeapify(minHeap, i);
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);
for (int i = 0; i < top; ++i)
printf("%d", arr[i]);
printf("\n");
}
}
// 哈夫曼编码主函数
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 decodeHuffman(struct MinHeapNode* root, char* s) {
struct MinHeapNode* curr = root;
while (*s) {
if (*s == '0')
curr = curr->left;
else
curr = curr->right;
if (isLeaf(curr)) {
printf("%c", curr->data);
curr = root;
}
++s;
}
}
// 计算信源熵
float calculateEntropy(int freq[], int size) {
float entropy = 0.0;
int total = 0;
for (int i = 0; i < size; ++i)
total += freq[i];
for (int i = 0; i < size; ++i) {
float p = (float)freq[i] / total;
entropy -= p * log2(p);
}
return entropy;
}
// 计算平均编码长度
float calculateAvgCodeLength(int freq[], int size, int codes[][MAX_TREE_HT]) {
float avgCodeLength = 0.0;
int total = 0;
for (int i = 0; i < size; ++i)
total += freq[i];
for (int i = 0; i < size; ++i) {
float p = (float)freq[i] / total;
int len = strlen((char*)codes[i]);
avgCodeLength += p * len;
}
return avgCodeLength;
}
int main() {
char data[] = {'a', 'b', 'c', 'd', 'e', 'f'};
int freq[] = {5, 9, 12, 13, 16, 45};
int size = sizeof(data) / sizeof(data[0]);
printf("Huffman Codes:\n");
HuffmanCodes(data, freq, size);
printf("\n");
char str[] = "101010000111110101011100";
printf("Decoded Huffman data: ");
decodeHuffman(buildHuffmanTree(data, freq, size), str);
printf("\n");
printf("Entropy: %f\n", calculateEntropy(freq, size));
int codes[size][MAX_TREE_HT];
for (int i = 0; i < size; ++i)
memset(codes[i], 0, sizeof(codes[i]));
int arr[MAX_TREE_HT], top = 0;
printCodes(buildHuffmanTree(data, freq, size), arr, top);
printf("Average Code Length: %f\n", calculateAvgCodeLength(freq, size, codes));
return 0;
}
```
阅读全文