用c语言创建哈夫曼编码表代码及注释
时间: 2024-05-13 21:15:45 浏览: 107
以下是用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;
}
// 创建一个容量为capacity的空的最小堆
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* buildHuffmanTree(char data[], int freq[], 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 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 HuffmanCodes(char data[], int freq[], int size) {
struct MinHeapNode* root = buildHuffmanTree(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;
}
```
代码注释:
1. 宏定义`MAX_TREE_HT`表示哈夫曼树的最大高度。
2. 定义哈夫曼树节点结构体`MinHeapNode`,包含字符、频率、左子节点和右子节点。
3. 定义哈夫曼树结构体`MinHeap`,包含最小堆中节点数量、容量和指向哈夫曼树节点的指针数组。
4. `newNode`函数创建一个新的哈夫曼树节点。
5. `createMinHeap`函数创建一个容量为capacity的空的最小堆。
6. `swapMinHeapNode`函数交换两个节点。
7. `minHeapify`函数是最小堆的堆化过程。
8. `isSizeOne`函数判断堆是否只有一个节点。
9. `extractMin`函数弹出最小频率的节点。
10. `insertMinHeap`函数插入新节点。
11. `isLeaf`函数判断是否为叶子节点。
12. `buildHuffmanTree`函数创建哈夫曼树。
13. `printCodes`函数打印哈夫曼编码。
14. `HuffmanCodes`函数创建哈夫曼编码表。
15. `main`函数测试代码。
阅读全文