基于哈夫曼树的数据压缩算法C语言
时间: 2023-06-29 08:17:19 浏览: 159
哈夫曼编码是一种数据压缩算法,可以将数据压缩成更小的数据量,以节省存储空间和传输带宽。下面是基于哈夫曼树的数据压缩算法的C语言实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_TREE_HT 100
#define MAX_CHAR 256
//哈夫曼树的结构体
struct MinHeapNode {
char data;
int freq;
struct MinHeapNode *left, *right;
};
struct MinHeap {
int size;
int capacity;
struct MinHeapNode **array;
};
//创建一个新的哈夫曼树节点
struct MinHeapNode* newNode(char data, int 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(int 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 compress(char* input_string, char* output_string) {
int freq[MAX_CHAR] = {0};
int n = strlen(input_string);
for (int i = 0; i < n; ++i)
++freq[input_string[i]];
struct MinHeapNode* root = buildHuffmanTree(input_string, freq, MAX_CHAR);
int arr[MAX_TREE_HT], top = 0;
printCodes(root, arr, top);
for (int i = 0; i < n; ++i) {
int j;
for (j = 0; j < MAX_CHAR; ++j)
if (input_string[i] == root->data)
break;
int k = 0;
while (k < top) {
output_string[k++] = arr[j++];
}
}
}
int main() {
char input_string[] = "Hello World";
char output_string[MAX_TREE_HT];
compress(input_string, output_string);
printf("Compressed string: %s\n", output_string);
return 0;
}
```
该算法主要包括以下步骤:
1. 创建一个哈夫曼树节点结构体,包括字符数据、字符出现频率、左子节点和右子节点。
2. 创建一个最小堆结构体,包括当前堆的大小、最大容量和哈夫曼树节点数组。
3. 实现最小堆的常用操作,包括维护最小堆性质、插入节点、获取最小节点等。
4. 构建哈夫曼树,将所有字符的频率作为节点的值,以最小堆的形式存储,并通过不断提取最小值构建哈夫曼树。
5. 打印哈夫曼编码表,遍历哈夫曼树,当遇到叶子节点时,输出该字符的编码。
6. 压缩数据,遍历输入字符串,找到对应字符的编码,并将编码存储到输出字符串中。
以上是哈夫曼编码的基本实现,实际应用中还需要考虑压缩文件头、解压缩等问题。
阅读全文