实现哈夫曼树和哈夫曼编码C语言
时间: 2023-11-24 11:57:48 浏览: 97
哈夫曼树的实现步骤如下:
1. 统计每个字符出现的次数,并将每个字符及其出现次数保存到一个结构体数组中。
2. 将结构体数组按照出现次数从小到大排序。
3. 创建一个空的二叉树。
4. 将结构体数组中出现次数最小的两个字符作为左右子树插入二叉树中,并将它们的出现次数相加作为新节点的出现次数。
5. 将新节点插入到结构体数组中,并重新按照出现次数从小到大排序。
6. 重复步骤4和5,直到结构体数组中只剩下一个节点,此节点即为哈夫曼树的根节点。
哈夫曼编码的实现步骤如下:
1. 遍历哈夫曼树,记录每个叶子节点的路径,路径上的0表示向左,1表示向右。
2. 将每个字符的编码保存到一个数组中。
以下是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* 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;
}
// 从哈夫曼树中取出出现次数最小的节点
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 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);
}
}
// 插入一个新的节点到哈夫曼树中
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;
}
// 构建哈夫曼树
struct MinHeapNode* buildHuffmanTree(char data[], int freq[], int size) {
struct MinHeapNode *left, *right, *top;
struct MinHeap* minHeap = createMinHeap(size);
for (int i = 0; i < size; ++i) {
insertMinHeap(minHeap, newNode(data[i], freq[i]));
}
while (minHeap->size != 1) {
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 saveCode(struct MinHeapNode* root, int arr[], int top) {
if (root->left) {
arr[top] = 0;
saveCode(root->left, arr, top + 1);
}
if (root->right) {
arr[top] = 1;
saveCode(root->right, arr, top + 1);
}
if (!root->left && !root->right) {
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;
saveCode(root, arr, top);
}
int main() {
char data[] = {'a', 'e', 'i', 'o', 'u'};
int freq[] = {10, 15, 12, 3, 4};
int size = sizeof(data) / sizeof(data[0]);
HuffmanCodes(data, freq, size);
return 0;
}
```
阅读全文