C语言实现哈夫曼树和哈夫曼编码
时间: 2023-11-24 17:10:48 浏览: 107
哈夫曼树是一种特殊的二叉树,它用于数据压缩和解压缩中的编码和解码。哈夫曼编码是一种前缀编码,它把不同字符映射到不同的二进制编码,使得编码后的字符串最小化。
下面是使用C语言实现哈夫曼树和哈夫曼编码的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_TREE_HT 100
typedef struct MinHeapNode{
char data;
unsigned freq;
struct MinHeapNode *left, *right;
} MinHeapNode;
typedef struct MinHeap{
unsigned size;
unsigned capacity;
MinHeapNode **array;
} MinHeap;
typedef struct HuffmanNode{
char data;
char *code;
struct HuffmanNode *left, *right;
} HuffmanNode;
MinHeapNode* newNode(char data, unsigned freq){
MinHeapNode* temp = (MinHeapNode*)malloc(sizeof(MinHeapNode));
temp->left = temp->right = NULL;
temp->data = data;
temp->freq = freq;
return temp;
}
MinHeap* createMinHeap(unsigned capacity){
MinHeap* minHeap = (MinHeap*)malloc(sizeof(MinHeap));
minHeap->size = 0;
minHeap->capacity = capacity;
minHeap->array = (MinHeapNode**)malloc(minHeap->capacity * sizeof(MinHeapNode*));
return minHeap;
}
void swapMinHeapNode(MinHeapNode** a, MinHeapNode** b){
MinHeapNode* t = *a;
*a = *b;
*b = t;
}
void minHeapify(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(MinHeap* minHeap){
return (minHeap->size == 1);
}
MinHeapNode* extractMin(MinHeap* minHeap){
MinHeapNode* temp = minHeap->array[0];
minHeap->array[0] = minHeap->array[minHeap->size - 1];
--minHeap->size;
minHeapify(minHeap, 0);
return temp;
}
void insertMinHeap(MinHeap* minHeap, 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;
}
void buildMinHeap(MinHeap* minHeap){
int n = minHeap->size - 1;
int i;
for (i = (n - 1) / 2; i >= 0; --i)
minHeapify(minHeap, i);
}
void printArr(int arr[], int n){
int i;
for (i = 0; i < n; ++i)
printf("%d", arr[i]);
printf("\n");
}
int isLeaf(MinHeapNode* root){
return !(root->left) && !(root->right);
}
MinHeap* createAndBuildMinHeap(char data[], int freq[], int size){
MinHeap* minHeap = createMinHeap(size);
for (int i = 0; i < size; ++i)
minHeap->array[i] = newNode(data[i], freq[i]);
minHeap->size = size;
buildMinHeap(minHeap);
return minHeap;
}
MinHeapNode* buildHuffmanTree(char data[], int freq[], int size){
MinHeapNode *left, *right, *top;
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(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);
printArr(arr, top);
}
}
void HuffmanCodes(char data[], int freq[], int size){
MinHeapNode* root = buildHuffmanTree(data, freq, size);
int arr[MAX_TREE_HT], top = 0;
printCodes(root, arr, top);
}
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);
return 0;
}
```
该代码实现了哈夫曼树和哈夫曼编码的基本功能。在这个例子中,我们使用了6个字符和它们的频率来构建哈夫曼树并打印出哈夫曼编码。
输出结果如下:
```
a: 0
c: 100
b: 101
f: 110
d: 1110
e: 1111
```
这些二进制编码可以用于压缩数据。
阅读全文