c语言哈夫曼编码译码
时间: 2024-05-06 20:14:30 浏览: 107
哈夫曼编码是一种用于压缩数据的技术,它的基本思想是根据数据出现的频率构建一棵二叉树,并将出现频率较高的字符编码为较短的二进制码,出现频率较低的字符编码为较长的二进制码,从而达到压缩数据的目的。哈夫曼编码的解码过程就是根据构建的二叉树进行反向遍历,将二进制码转换为原来的字符。
哈夫曼编码的具体步骤如下:
1. 统计待压缩数据中每个字符出现的频率,将其保存在一个字符频率表中。
2. 将字符频率表中的每个字符作为叶子节点,构建哈夫曼树。构建哈夫曼树的过程可以采用贪心算法,每次选取频率最小的两个节点合并。
3. 对哈夫曼树进行编码,从根节点开始遍历哈夫曼树,遇到左子节点为0,遇到右子节点为1。将每个字符的编码保存在一个编码表中。
4. 将待压缩数据中的每个字符转换为其对应的哈夫曼编码,并将所有编码拼接起来,形成压缩后的数据。
哈夫曼解码的具体步骤如下:
1. 读取压缩后的数据,并从根节点开始遍历哈夫曼树。
2. 遇到0则遍历左子节点,遇到1则遍历右子节点,直到遍历到叶子节点。
3. 根据叶子节点对应的字符进行解码,并将解码后的字符保存起来,直到所有编码都被解码完毕。
相关问题
c语言哈夫曼编码译码器课设,数据结构课程设计哈夫曼编码译码器
哈夫曼编码是一种压缩算法,它通过对原始数据进行编码,可以把数据压缩为更小的体积,从而减少存储空间和传输带宽的占用。C语言实现哈夫曼编码译码器的关键在于,需要用哈夫曼树来生成编码表,然后利用编码表来对数据进行编码和解码。
以下是一个简单的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 *temp = (struct MinHeapNode *)malloc(sizeof(struct MinHeapNode));
temp->left = temp->right = NULL;
temp->data = data;
temp->freq = freq;
return temp;
}
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);
int i;
for (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);
}
int main() {
char arr[] = {'a', 'b', 'c', 'd', 'e', 'f'};
int freq[] = {5, 9, 12, 13, 16, 45};
int size = sizeof(arr) / sizeof(arr[0]);
HuffmanCodes(arr, freq, size);
return 0;
}
```
这段代码定义了一个`MinHeapNode`结构体表示哈夫曼树的节点,`MinHeap`结构体表示最小堆,其中`array`数组存储了指向哈夫曼树节点的指针。`newNode`函数用于创建一个新的哈夫曼树节点,`createMinHeap`函数用于创建一个最小堆,`swapMinHeapNode`函数用于交换两个最小堆节点的位置,`minHeapify`函数用于维护最小堆的性质,`isSizeOne`函数用于判断最小堆的大小是否为1,`extractMin`函数用于取出最小堆的根节点,`insertMinHeap`函数用于插入一个新的节点到最小堆中,`buildMinHeap`函数用于构建最小堆,`printArr`函数用于打印一个整型数组,`isLeaf`函数用于判断一个节点是否为叶子节点,`createAndBuildMinHeap`函数用于创建并构建一个最小堆,`buildHuffmanTree`函数用于构建哈夫曼树,`printCodes`函数用于打印哈夫曼编码,`HuffmanCodes`函数用于生成哈夫曼编码。
你可以根据自己的需要对这段代码进行修改和补充,以实现一个完整的哈夫曼编码译码器。
用c语言哈夫曼编码和译码
以下是使用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* temp = (struct MinHeapNode*)malloc(sizeof(struct MinHeapNode));
temp->left = temp->right = NULL;
temp->data = data;
temp->freq = freq;
return temp;
}
// 创建一个新的哈夫曼树
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 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 = (minHeap->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 (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);
}
// 哈夫曼译码主函数
void decodeHuffman(struct MinHeapNode* root, char* s) {
struct MinHeapNode* curr = root;
while (*s) {
if (*s == '0')
curr = curr->left;
else
curr = curr->right;
if (isLeaf(curr)) {
printf("%c", curr->data);
curr = root;
}
++s;
}
}
// 计算信源熵
float calculateEntropy(int freq[], int size) {
float entropy = 0.0;
int total = 0;
for (int i = 0; i < size; ++i)
total += freq[i];
for (int i = 0; i < size; ++i) {
float p = (float)freq[i] / total;
entropy -= p * log2(p);
}
return entropy;
}
// 计算平均编码长度
float calculateAvgCodeLength(int freq[], int size, int codes[][MAX_TREE_HT]) {
float avgCodeLength = 0.0;
int total = 0;
for (int i = 0; i < size; ++i)
total += freq[i];
for (int i = 0; i < size; ++i) {
float p = (float)freq[i] / total;
int len = strlen((char*)codes[i]);
avgCodeLength += p * len;
}
return avgCodeLength;
}
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]);
printf("Huffman Codes:\n");
HuffmanCodes(data, freq, size);
printf("\n");
char str[] = "101010000111110101011100";
printf("Decoded Huffman data: ");
decodeHuffman(buildHuffmanTree(data, freq, size), str);
printf("\n");
printf("Entropy: %f\n", calculateEntropy(freq, size));
int codes[size][MAX_TREE_HT];
for (int i = 0; i < size; ++i)
memset(codes[i], 0, sizeof(codes[i]));
int arr[MAX_TREE_HT], top = 0;
printCodes(buildHuffmanTree(data, freq, size), arr, top);
printf("Average Code Length: %f\n", calculateAvgCodeLength(freq, size, codes));
return 0;
}
```
阅读全文