哈夫曼编码译码c语言
时间: 2023-07-13 18:13:41 浏览: 140
赫夫曼编码译码器c语言
4星 · 用户满意度95%
下面是一个简单的哈夫曼编码和译码的 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 HuffmanCode {
char data;
char *code;
};
// 哈夫曼编码树结构体
struct MinHeap {
unsigned size;
unsigned capacity;
struct MinHeapNode **array;
};
// 创建新的哈夫曼树节点
struct MinHeapNode *newNode(char data, unsigned freq) {
struct MinHeapNode *node = (struct MinHeapNode *)malloc(sizeof(struct MinHeapNode));
node->left = node->right = NULL;
node->data = data;
node->freq = freq;
return node;
}
// 创建新的哈夫曼编码树
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 MinHeapNode *buildMinHeap(int *freq, char *data, int size) {
struct MinHeapNode *left, *right, *top;
struct MinHeap *minHeap = createMinHeap(size);
for (int i = 0; i < size; ++i) {
minHeap->array[i] = newNode(data[i], freq[i]);
}
minHeap->size = 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 generateHuffmanCodes(struct MinHeapNode *root, char *code, int top, struct HuffmanCode *huffmanCodes) {
if (root->left) {
code[top] = '0';
generateHuffmanCodes(root->left, code, top + 1, huffmanCodes);
}
if (root->right) {
code[top] = '1';
generateHuffmanCodes(root->right, code, top + 1, huffmanCodes);
}
if (isLeaf(root)) {
huffmanCodes[root->data].code = (char *)malloc((top + 1) * sizeof(char));
strcpy(huffmanCodes[root->data].code, code);
huffmanCodes[root->data].data = root->data;
}
}
// 哈夫曼编码函数
void huffmanEncode(char *data, int *freq, int size) {
struct MinHeapNode *root = buildMinHeap(freq, data, size);
char code[MAX_TREE_HT], dataArr[size];
struct HuffmanCode *huffmanCodes = (struct HuffmanCode *)malloc(size * sizeof(struct HuffmanCode));
generateHuffmanCodes(root, code, 0, huffmanCodes);
printf("Huffman Codes:\n");
for (int i = 0; i < size; ++i) {
printf("%c: %s\n", huffmanCodes[data[i]].data, huffmanCodes[data[i]].code);
}
}
// 哈夫曼解码函数
void huffmanDecode(struct MinHeapNode *root, char *code, int index) {
if (!root) {
return;
}
if (isLeaf(root)) {
printf("%c", root->data);
return;
}
++index;
if (code[index] == '0') {
huffmanDecode(root->left, code, index);
} else {
huffmanDecode(root->right, code, index);
}
}
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]);
huffmanEncode(data, freq, size);
struct MinHeapNode *root = buildMinHeap(freq, data, size);
printf("\nDecoded data: ");
char code[] = "11000101111110100011101001";
int index = -1;
while (index < (int)strlen(code) - 2) {
huffmanDecode(root, code, index);
}
return 0;
}
```
这个示例中,包含了创建哈夫曼树节点、创建哈夫曼编码树、交换哈夫曼树节点、维护最小堆的性质、获取最小频率的哈夫曼树节点、插入新的哈夫曼树节点、判断是否为叶节点、生成哈夫曼编码、哈夫曼编码函数、哈夫曼解码函数等多个函数,可以根据自己的需求进行修改和完善。
阅读全文