贪心算法求哈夫曼编码
时间: 2023-11-10 13:04:35 浏览: 96
哈夫曼编码是一种可变长度编码,用于将字符转换为二进制编码。贪心算法可以用来求解哈夫曼编码。
具体步骤如下:
1. 统计每个字符出现的频率,并将它们存储在一个优先队列中。
2. 从队列中取出频率最小的两个字符,将它们合并成一个新的节点,并将这个节点的频率设置为两个字符的频率之和。
3. 将新节点插入到队列中。
4. 重复步骤2和3,直到队列中只剩下一个节点为止。
5. 从根节点开始,对树进行遍历,每当遇到一个左子节点时,将当前编码加上0,每当遇到一个右子节点时,将当前编码加上1。最终得到每个字符的哈夫曼编码。
相关问题
贪心算法求哈夫曼编码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* 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);
}
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);
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);
}
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;
}
```
这个代码实现了一个哈夫曼编码器,按照频率构建了一个哈夫曼树,并输出了每个字符的哈夫曼编码。
贪心算法求哈夫曼编码C++
哈夫曼编码是一种可变长度编码,用于无损数据压缩。贪心算法可以用来求解哈夫曼编码。具体步骤如下:
1. 统计每个字符出现的频率,并将它们存储在一个数组中。
2. 创建一个最小堆,并将所有的字符节点插入其中。每个节点包含一个字符和它的频率。
3. 从最小堆中取出两个频率最小的节点,合并它们,并将新节点插入回最小堆中。新节点的频率为两个旧节点的频率之和。
4. 重复步骤3,直到最小堆中只剩下一个节点为止。这个节点就是哈夫曼树的根节点。
5. 遍历哈夫曼树,给左子树编码为0,给右子树编码为1。从根节点开始,每当向左走一步就在编码末尾添加一个0,每当向右走一步就在编码末尾添加一个1。最终得到每个字符的哈夫曼编码。
以下是C++代码实现:
```c++
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct Node {
char ch;
int freq;
Node* left;
Node* right;
Node(char c, int f) : ch(c), freq(f), left(nullptr), right(nullptr) {}
};
struct Compare {
bool operator()(Node* a, Node* b) {
return a->freq > b->freq;
}
};
void encode(Node* root, string code, vector<string>& codes) {
if (!root) return;
if (root->ch != '#') codes[root->ch] = code;
encode(root->left, code + "0", codes);
encode(root->right, code + "1", codes);
}
vector<string> huffman(vector<int>& freqs) {
priority_queue<Node*, vector<Node*>, Compare> pq;
for (int i = 0; i < freqs.size(); i++) {
if (freqs[i] > 0) {
pq.push(new Node(i, freqs[i]));
}
}
while (pq.size() > 1) {
Node* left = pq.top(); pq.pop();
Node* right = pq.top(); pq.pop();
Node* parent = new Node('#', left->freq + right->freq);
parent->left = left;
parent->right = right;
pq.push(parent);
}
vector<string> codes(256);
encode(pq.top(), "", codes);
return codes;
}
int main() {
string s = "hello world";
vector<int> freqs(256, 0);
for (char c : s) freqs[c]++;
vector<string> codes = huffman(freqs);
for (int i = 0; i < 256; i++) {
if (freqs[i] > 0) {
cout << (char)i << ": " << codes[i] << endl;
}
}
return 0;
}
```
阅读全文