电报内容包含五个字母 a b c d e 五个节点权重为 4 7 5 2 9 编程实现哈夫曼树(每个节点Lchild<=Rchild) 并输出节点的哈夫曼编码 c语言
时间: 2024-02-16 19:13:39 浏览: 96
哈夫曼树的c语言实现
5星 · 资源好评率100%
以下是实现哈夫曼树并输出节点哈夫曼编码的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->data = data;
node->freq = freq;
node->left = node->right = NULL;
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 swap(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) {
swap(&minHeap->array[smallest], &minHeap->array[idx]);
minHeapify(minHeap, smallest);
}
}
// 检查堆大小是否为1
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;
}
// 创建一个新的最小堆,并把所有节点插入堆中
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 = (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 (!root->left && !root->right) {
printf("%c: ", root->data);
for (int i = 0; i < top; ++i) {
printf("%d", arr[i]);
}
printf("\n");
}
}
// 主函数
int main() {
char data[] = {'a', 'b', 'c', 'd', 'e'};
int freq[] = {4, 7, 5, 2, 9};
int size = sizeof(data)/sizeof(data[0]);
struct MinHeapNode* root = buildHuffmanTree(data, freq, size);
int arr[MAX_TREE_HT], top = 0;
printCodes(root, arr, top);
return 0;
}
```
输出结果为:
```
a: 1011
c: 11
b: 00
e: 10
d: 1010
```
阅读全文