给个范式Huffman编码的c代码
时间: 2023-09-21 22:05:45 浏览: 138
### 回答1:
我提供一段简单的 Huffman 编码 C 代码:#include <stdio.h>
#include <stdlib.h>
struct node {
char data;
int freq;
struct node * left;
struct node * right;
};
struct node * huffmanTree(char data[], int freq[], int size)
{
struct node * left, * right, * top;
// 建立最小堆
for (int i = size / 2 - 1; i >= 0; i--)
{
int j = i;
while (j <= size / 2 - 1)
{
int k = 2 * j + 1;
if (k + 1 < size && freq[k] > freq[k + 1])
k++;
if (freq[j] <= freq[k])
break;
int temp = freq[k];
freq[k] = freq[j];
freq[j] = temp;
char c = data[k];
data[k] = data[j];
data[j] = c;
j = k;
}
}
// 建立 Huffman 树
for (int i = 1; i <= size - 1; i++)
{
left = (struct node*)malloc(sizeof(struct node));
right = (struct node*)malloc(sizeof(struct node));
// 取出两个最小的节点
left->data = data[0];
left->freq = freq[0];
left->left = left->right = NULL;
right->data = data[1];
right->freq = freq[1];
right->left = right->right = NULL;
freq[0] = freq[0] + freq[1];
freq[1] = freq[size - 1];
size--;
// 重新建立最小堆
int j = 0;
while (j <= size / 2 - 1)
{
int k = 2 * j + 1;
if (k + 1 < size && freq[k] > freq[k + 1])
k++;
if (freq[j] <= freq[k])
break;
int temp = freq[k];
freq[k] = freq[j];
freq[j] = temp;
char c = data[k];
data[k] = data[j];
data[j] = c;
j = k;
}
top = (struct node*)malloc(sizeof(struct node));
top->freq = left->freq + right->freq;
top->data = '$';
top->left = left;
top->right = right;
}
return top;
}
### 回答2:
Huffman编码是一种无损压缩算法,用于对数据进行编码,以减少数据传输和存储的成本。下面是一个示例的Huffman编码的C代码范式:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 结点结构体
typedef struct Node {
unsigned char data; // 字符数据
int frequency; // 字符出现的频率
struct Node *left, *right; // 左右子节点
} Node;
// 创建新结点
Node* createNode(unsigned char data, int frequency)
{
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->frequency = frequency;
newNode->left = newNode->right = NULL;
return newNode;
}
// 构建Huffman树
Node* buildHuffmanTree(unsigned char data[], int frequency[], int size)
{
Node *left, *right, *top;
// 创建一个优先队列,用于存储Huffman树的结点
PriorityQueue* pq = createPriorityQueue();
// 将每个字符的频率作为优先级,构建Huffman树的结点
for (int i = 0; i < size; ++i) {
enqueue(pq, createNode(data[i], frequency[i]));
}
// 直到只剩下一个结点,即为Huffman树的根结点
while (getSize(pq) > 1) {
left = dequeue(pq);
right = dequeue(pq);
top = createNode('$', left->frequency + right->frequency); // $表示内部结点
top->left = left;
top->right = right;
enqueue(pq, top);
}
return dequeue(pq);
}
// 递归地打印Huffman编码
void printHuffmanCodes(Node* root, char arr[], int top)
{
// 叶子结点的编码位于arr[]数组中
if (root->left) {
arr[top] = '0';
printHuffmanCodes(root->left, arr, top + 1);
}
if (root->right) {
arr[top] = '1';
printHuffmanCodes(root->right, arr, top + 1);
}
// 只有一个字符的结点时,打印编码
if (isLeaf(root)) {
printf("%c: ", root->data);
for (int i = 0; i < top; ++i) {
printf("%c", arr[i]);
}
printf("\n");
}
}
// Huffman编码的主函数
void huffmanCoding(unsigned char data[], int frequency[], int size)
{
// 构建Huffman树
Node* root = buildHuffmanTree(data, frequency, size);
// 用于存储编码的临时数组
char arr[MAX_TREE_HT], top = 0;
// 打印Huffman编码
printHuffmanCodes(root, arr, top);
}
int main()
{
unsigned char data[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
int frequency[] = { 5, 9, 12, 13, 16, 45 };
int size = sizeof(data) / sizeof(data[0]);
// 运行Huffman编码
huffmanCoding(data, frequency, size);
return 0;
}
```
这是一个简单的Huffman编码的C代码范式示例。注意,代码中使用了一些未定义的函数和数据结构,如`PriorityQueue`、`enqueue`等,需要根据实际情况进行定义和实现。此外,还需要添加一些错误检查和释放内存的代码,以确保程序的稳定性和健壮性。
### 回答3:
以下是一个用C语言实现的Huffman编码的范例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.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);
}
}
bool 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");
}
bool 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);
}
int main()
{
int n;
printf("请输入字符的数量: ");
scanf("%d", &n);
char data[n];
int freq[n];
printf("请输入字符及其频率:\n");
for (int i = 0; i < n; i++) {
scanf("%s %d", &data[i], &freq[i]);
}
int size = sizeof(data) / sizeof(data[0]);
printf("Huffman Codes:\n");
HuffmanCodes(data, freq, size);
return 0;
}
```
以上是一个使用C语言实现的Huffman编码的范例代码,通过输入字符及其频率,可以输出对应的Huffman编码。
阅读全文