Huffman 编码的编程与实现C语言
时间: 2024-05-13 22:18:51 浏览: 66
Huffman 编码是一种基于字符频率的压缩算法,将出现频率高的字符用较短的编码表示,出现频率低的字符用较长的编码表示,从而实现数据压缩。以下是 Huffman 编码的 C 语言实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_TREE_HT 100
// Huffman树节点
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* 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);
}
// 创建一个最小堆并构建Huffman树
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;
while (!isSizeOne(minHeap)) {
struct MinHeapNode* left = extractMin(minHeap);
struct MinHeapNode* right = extractMin(minHeap);
struct MinHeapNode* top =
newNode('$', left->freq + right->freq);
top->left = left;
top->right = right;
insertMinHeap(minHeap, top);
}
return minHeap;
}
// 递归地打印Huffman编码
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");
}
}
// Huffman编码
void HuffmanCodes(char data[], int freq[], int size) {
struct MinHeapNode* root = createAndBuildMinHeap(data, freq, size);
int arr[MAX_TREE_HT], top = 0;
printCodes(root, arr, top);
}
// 主函数
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]);
HuffmanCodes(data, freq, size);
return 0;
}
```
以上代码中,我们定义了 Huffman 树节点,最小堆和对应的操作。在主函数中,我们定义了一些字符和它们出现的频率,然后调用 HuffmanCodes 函数来生成对应的 Huffman 编码。在函数内部,我们首先创建一个包含所有字符节点的最小堆,然后构建 Huffman 树,并使用 printCodes 函数递归地计算每个字符的编码并打印出来。
阅读全文