Huffman编码是一种用于数据压缩的算法,通过构建一棵Huffman树来为每个字符分配一个唯一的二进制编码。Huffman树的构建基于字符出现的频率,频率越高的字符编码越短,从而达到压缩数据的目的。
Huffman编码主要分为两种类型:
Huffman编码广泛应用于数据通信、文件压缩等领域,特别是在需要高效压缩且保证数据完整性的场景中。
以下是用C语言从给定的Huffman树生成Huffman代码的示例代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int weight;
int parent, lchild, rchild;
} HuffmanNode, *HuffmanTree;
typedef char **HuffmanCode;
void Select(HuffmanTree HT, int k, int *s1, int *s2) {
int min;
for (int i = 1; i <= k; i++) {
if (HT[i].parent == 0) {
min = i;
break;
}
}
for (int j = i + 1; j <= k; j++) {
if (HT[j].parent == 0 && HT[j].weight < HT[min].weight) {
min = j;
}
}
*s1 = min;
for (int i = 1; i <= k; i++) {
if (HT[i].parent == 0 && i != (*s1)) {
min = i;
break;
}
}
for (int j = i + 1; j <= k; j++) {
if (HT[j].parent == 0 && HT[j].weight < HT[min].weight && j != (*s1)) {
min = j;
}
}
*s2 = min;
}
void HuffmanCoding(HuffmanTree &HT, HuffmanCode &HC, int *w, int n) {
int m = 2 * n - 1;
HT = (HuffmanTree)malloc((m + 1) * sizeof(HuffmanNode));
HC = (HuffmanCode)malloc((n + 1) * sizeof(char *));
for (int i = 1; i <= m; i++) {
HT[i].parent = 0;
HT[i].lchild = 0;
HT[i].rchild = 0;
}
for (int i = 1; i <= n; i++) {
HT[i].weight = w[i - 1];
}
for (int i = n + 1; i <= m; i++) {
Select(HT, i - 1, &s1, &s2);
HT[s1].parent = i;
HT[s2].parent = i;
HT[i].lchild = s1;
HT[i].rchild = s2;
HT[i].weight = HT[s1].weight + HT[s2].weight;
}
for (int i = 1; i <= n; i++) {
int start = n;
char cd[n];
cd[n - 1] = '\0';
int c = i;
int f = HT[i].parent;
while (f != 0) {
start--;
if (HT[f].lchild == c) {
cd[start] = '0';
} else {
cd[start] = '1';
}
c = f;
f = HT[f].parent;
}
HC[i] = (char *)malloc((n - start) * sizeof(char));
strcpy(HC[i], &cd[start]);
}
}
int main() {
int w[] = {5, 29, 7, 8, 14, 23, 3, 11};
int n = sizeof(w) / sizeof(w[0]);
HuffmanTree HT;
HuffmanCode HC;
HuffmanCoding(HT, HC, w, n);
for (int i = 1; i <= n; i++) {
printf("字符 %d 的 Huffman 编码是: %s\n", i, HC[i]);
}
return 0;
}
malloc
分配内存时,确保分配的内存大小正确,并在使用完后释放内存以避免内存泄漏。通过以上步骤和示例代码,你可以从给定的Huffman树生成对应的Huffman编码。
领取专属 10元无门槛券
手把手带您无忧上云