利用C语言编写软件实现上述功能。具体要求为:从键盘任意输入一个由英文字母构成的文本字符串,例如“ilikelearningdatastructurecourse”,首先根据该字符串创建Huffman树,之后进行Huffman编码,然后把该字符串的Huffman编码结果向屏幕输出。能够利用前述的Huffman编码结果对某0-1码流进行解码得到字符串
时间: 2024-03-07 09:49:52 浏览: 69
好的,以下是C语言实现Huffman编码和解码的代码:
```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;
};
// Huffman树根节点
struct MinHeap {
unsigned size;
unsigned capacity;
struct MinHeapNode **array;
};
// 创建一个新的单个字符节点
struct MinHeapNode* newNode(char data, unsigned freq) {
struct MinHeapNode* temp = (struct MinHeapNode*) malloc(sizeof(struct MinHeapNode));
temp->left = temp->right = NULL;
temp->data = data;
temp->freq = freq;
return temp;
}
// 创建一个新的Huffman树
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);
}
}
// 判断最小堆大小是否为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;
}
// 构建Huffman树
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 (!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);
}
// 判断节点是否为叶子节点
int isLeaf(struct MinHeapNode* root) {
return !(root->left) && !(root->right);
}
// 输出Huffman编码
void printHuffmanCode(struct MinHeapNode* root, int arr[], int top) {
if (root->left) {
arr[top] = 0;
printHuffmanCode(root->left, arr, top + 1);
}
if (root->right) {
arr[top] = 1;
printHuffmanCode(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");
}
}
// 获取Huffman编码
void getHuffmanCode(char data[], int freq[], int size) {
struct MinHeapNode* root = buildHuffmanTree(data, freq, size);
int arr[MAX_TREE_HT], top = 0;
printHuffmanCode(root, arr, top);
}
// 编码单个字符
void encodeChar(struct MinHeapNode* root, char ch, int code[], int top) {
if (root->left) {
code[top] = 0;
encodeChar(root->left, ch, code, top + 1);
}
if (root->right) {
code[top] = 1;
encodeChar(root->right, ch, code, top + 1);
}
if (root->data == ch) {
printf("0b");
for (int i = 0; i < top; i++) {
printf("%d", code[i]);
}
printf("\n");
}
}
// 编码字符串
void encodeString(struct MinHeapNode* root, char str[], int n) {
int code[MAX_TREE_HT], top = 0;
for (int i = 0; i < n; i++) {
encodeChar(root, str[i], code, top);
}
}
// 解码单个字符
void decodeChar(struct MinHeapNode* root, char ch, int i) {
if (root->data != '$') {
printf("%c", root->data);
return;
}
if (ch == '0') {
decodeChar(root->left, ch, i + 1);
} else {
decodeChar(root->right, ch, i + 1);
}
}
// 解码字符串
void decodeString(struct MinHeapNode* root, char str[], int n) {
for (int i = 0; i < n; i++) {
decodeChar(root, str[i], 0);
}
printf("\n");
}
int main() {
char data[] = "ilikelearningdatastructurecourse";
int freq[] = {4, 1, 2, 2, 1, 1, 2, 1, 1, 2, 1, 1, 3, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
int size = sizeof(data) / sizeof(data[0]);
getHuffmanCode(data, freq, size);
char str[] = "ilike";
int n = strlen(str);
printf("Encoding of \"%s\" is:\n", str);
encodeString(buildHuffmanTree(data, freq, size), str, n);
char code[] = "0100001111110110001011010101100100110001000";
int m = strlen(code);
printf("Decoding of \"%s\" is:\n", code);
decodeString(buildHuffmanTree(data, freq, size), code, m);
return 0;
}
```
以上代码中,`buildHuffmanTree`函数用于构建Huffman树,`printHuffmanCode`函数用于输出Huffman编码,`encodeChar`函数用于编码单个字符,`encodeString`函数用于编码字符串,`decodeChar`函数用于解码单个字符,`decodeString`函数用于解码字符串。在`main`函数中,首先创建了一个Huffman树,并输出了每个字符的Huffman编码。接着对字符串进行编码,最后对某个0-1码流进行解码得到字符串。
阅读全文