请用c语言编写以下代码:2. 哈夫曼编码 任务:根据电文中各字符出现的频率,建立哈夫曼树,并设计字符的哈夫曼编码。 功能要求: 1) 建立并输出哈夫曼树的存储结构; 2) 根据哈夫曼树,设计并输出对应字符的哈夫曼编码; 3) 将指定接收到的电文进行译码并输出。 界面要求:程序运行后,给出菜单项的内容和输入提示: 1. 建立哈夫曼树 2. 哈夫曼编码 3. 译码 0. 退出 请选择0-3:
时间: 2024-03-25 21:35:38 浏览: 186
以下是用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;
}
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;
}
void buildMinHeap(struct 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(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;
buildMinHeap(minHeap);
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);
printArr(arr, top);
}
}
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);
}
void decodeHuffman(struct MinHeapNode *root, char *string) {
struct MinHeapNode *curr = root;
printf("Decoded string: ");
for (int i = 0; i < strlen(string); ++i) {
if (string[i] == '0') {
curr = curr->left;
} else {
curr = curr->right;
}
if (isLeaf(curr)) {
printf("%c", curr->data);
curr = root;
}
}
printf("\n");
}
int main() {
char data[100];
int freq[100];
int choice = -1;
int size = 0;
while (choice != 0) {
printf("1. Build Huffman tree\n");
printf("2. Huffman encoding\n");
printf("3. Decoding\n");
printf("0. Exit\n");
printf("Please choose 0-3: ");
scanf("%d", &choice);
switch (choice) {
case 1: {
printf("Enter the number of characters: ");
scanf("%d", &size);
printf("Enter the characters and their frequencies:\n");
for (int i = 0; i < size; ++i) {
scanf(" %c %d", &data[i], &freq[i]);
}
printf("Huffman tree:\n");
struct MinHeapNode *root = buildHuffmanTree(data, freq, size);
printf("Data: %c, Freq: %d\n", root->data, root->freq);
break;
}
case 2: {
printf("Enter the number of characters: ");
scanf("%d", &size);
printf("Enter the characters and their frequencies:\n");
for (int i = 0; i < size; ++i) {
scanf(" %c %d", &data[i], &freq[i]);
}
HuffmanCodes(data, freq, size);
break;
}
case 3: {
char string[100];
printf("Enter the encoded string: ");
scanf("%s", string);
printf("Enter the characters and their frequencies:\n");
for (int i = 0; i < size; ++i) {
scanf(" %c %d", &data[i], &freq[i]);
}
struct MinHeapNode *root = buildHuffmanTree(data, freq, size);
decodeHuffman(root, string);
break;
}
case 0: {
break;
}
default: {
printf("Invalid choice\n");
break;
}
}
}
return 0;
}
```
这个程序可以实现建立哈夫曼树、哈夫曼编码、以及译码的功能。在程序运行后,会给出菜单项的内容和输入提示,让用户选择要进行的操作。用户可以输入字符和它们的频率来建立哈夫曼树,然后程序会输出哈夫曼树的存储结构。用户也可以输入字符和它们的频率来生成哈夫曼编码,然后程序会输出对应字符的哈夫曼编码。最后,用户可以输入一个经过编码的字符串,程序会根据哈夫曼树进行译码,并输出原始字符串。
阅读全文