编程实现赫夫曼编码的流程
时间: 2024-04-14 12:14:20 浏览: 99
赫夫曼编码是一种基于字符出现频率的压缩算法,可以将常见字符用较少的比特表示,而不常见字符用较多的比特表示,从而减小数据的存储空间。下面是赫夫曼编码的实现流程:
1. 统计字符出现频率:遍历待压缩数据,统计各个字符出现的次数,记录在一个频率表中。
2. 构建哈夫曼树:将频率表中的每个字符看作一个节点,构建哈夫曼树。哈夫曼树是一棵二叉树,每个节点有一个权值,叶子节点对应一个字符,非叶子节点的权值是其左右子树权值之和。构建哈夫曼树的方法是:先将所有节点按权值从小到大排序,然后取出权值最小的两个节点构建一个新节点,将这个新节点插入到原来的节点列表中,重复以上步骤,直到只剩下一个节点,这个节点就是哈夫曼树的根节点。
3. 生成编码表:从哈夫曼树的根节点开始遍历,如果往左走,就给当前编码加上一个 0,如果往右走,就给当前编码加上一个 1。当遇到叶子节点时,就将该节点对应的字符和其编码存入编码表中。
4. 进行压缩:将待压缩数据重新遍历一遍,将每个字符用其对应的编码替换,得到压缩后的数据。
5. 进行解压缩:使用相同的哈夫曼树和编码表,将压缩后的数据重新转换为原始数据。
实现赫夫曼编码可以使用各种编程语言,例如 Python、C++、Java 等。
相关问题
你好,请用C语言完整的编程实现赫夫曼编码,并写出赫夫曼编码的步骤以及流程图
好的,下面是C语言完整的赫夫曼编码实现,以及赫夫曼编码的步骤和流程图。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_TREE_HT 100
typedef struct MinHeapNode {
char data;
unsigned freq;
struct MinHeapNode *left, *right;
} MinHeapNode;
typedef struct MinHeap {
unsigned size;
unsigned capacity;
struct MinHeapNode** array;
} MinHeap;
MinHeapNode* newNode(char data, unsigned freq) {
MinHeapNode* node = (MinHeapNode*)malloc(sizeof(MinHeapNode));
node->left = node->right = NULL;
node->data = data;
node->freq = freq;
return node;
}
MinHeap* createMinHeap(unsigned capacity) {
MinHeap* minHeap = (MinHeap*)malloc(sizeof(MinHeap));
minHeap->size = 0;
minHeap->capacity = capacity;
minHeap->array = (MinHeapNode**)malloc(minHeap->capacity * sizeof(MinHeapNode*));
return minHeap;
}
void swapMinHeapNode(MinHeapNode** a, MinHeapNode** b) {
MinHeapNode* t = *a;
*a = *b;
*b = t;
}
void minHeapify(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(MinHeap* minHeap) {
return (minHeap->size == 1);
}
MinHeapNode* extractMin(MinHeap* minHeap) {
MinHeapNode* temp = minHeap->array[0];
minHeap->array[0] = minHeap->array[minHeap->size - 1];
--minHeap->size;
minHeapify(minHeap, 0);
return temp;
}
void insertMinHeap(MinHeap* minHeap, 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(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(MinHeapNode* root) {
return !(root->left) && !(root->right);
}
MinHeap* createAndBuildMinHeap(char data[], int freq[], int size) {
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;
}
MinHeapNode* buildHuffmanTree(char data[], int freq[], int size) {
MinHeapNode *left, *right, *top;
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(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) {
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. 统计字符出现的频率;
2. 将每个字符及其频率存储在一个节点中,并以节点的形式构建一个小根堆;
3. 从堆中取出最小的两个节点,合并成一个新节点,其权值为两个节点的权值之和,该节点的左右子节点分别是原来的两个节点,将新节点插入堆中;
4. 重复步骤3,直到堆中只剩下一个节点;
5. 从根节点开始,赋值为0或1,向左为0,向右为1,得到每个字符的编码;
6. 对于每个字符,输出其编码。
赫夫曼编码的流程图如下:
![Huffman编码流程图](https://img-blog.csdn.net/20171012005520569?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGl1aGFvXzIwMTcwNDE5/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/q/85)
阅读全文