我一直在看用c语言进行霍夫曼编码的熵编码和解码的代码,我似乎找不到为什么我总是得到分割错误的原因。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const int 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;
minHeap->array[i] = minHeapNode;
while (i && minHeap->array[i]->freq < minHeap->array[(i - 1) / 2]->freq) {
swapMinHeapNode(&minHeap->array[i], &minHeap->array[(i - 1) / 2]);
i = (i - 1) / 2;
}
}
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);
}
void encode(char* str, char* encoded_str, struct MinHeapNode* root) {
if (root == NULL)
return;
if (isLeaf(root)) {
while (*str != root->data)
str++;
while (*str == root->data) {
*encoded_str = '1';
encoded_str++;
str++;
}
return encode(str, encoded_str, root);
}
if (*str == root->data) {
*encoded_str = '0';
encoded_str++;
encode(str + 1, encoded_str, root->left);
} else {
encode(str, encoded_str, root->right);
}
}
void decode(char* encoded_str, struct MinHeapNode* root, char* decoded_str) {
struct MinHeapNode* curr = root;
while (*encoded_str) {
if (*encoded_str == '0')
curr = curr->left;
else
curr = curr->right;
if (isLeaf(curr)) {
*decoded_str = curr->data;
decoded_str++;
curr = root;
}
encoded_str++;
}
}
int main() {
char str[] = "this is a test";
int freq[256] = { 0 };
int size = 0, i;
for (i = 0; str[i]; i++) {
freq[str[i]]++;
size++;
}
int unique = 0;
char data[size];
for (i = 0; i < 256; i++) {
if (freq[i]) {
data[unique] = (char)i;
unique++;
}
}
HuffmanCodes(data, freq, unique);
char encoded_str[100], decoded_str[100];
struct MinHeapNode* root = buildHuffmanTree(data, freq, unique);
encode(str, encoded_str, root);
printf("Encoded string: %s\n", encoded_str);
decode(encoded_str, root, decoded_str);
printf("Decoded string: %s\n", decoded_str);
return 0;
}
经过几次逐节注解的尝试和错误,我们发现它是在编码函数内部的某个地方引起的。这不是我自己写的代码,是用一些在线参考编写的。输出应该是下面的。
: 00
a: 010
e: 011
h: 100
i: 1010
s: 1011
t: 110
Encoded string: 0101110101011101110111111001101
Decoded string: this is a test
相反,我得到了以下内容。
a: 0
e: 10
h: 110
i: 1110
s: 11110
: 111110
t: 111111
zsh: segmentation fault
先谢谢你的帮助。
2条答案
按热度按时间np8igboo1#
}
只需在while循环中添加一个条件,以确保字符串不会溢出:
但是!首先你需要解决的事实是:字符编码字符串[100]、解码字符串[100];都是空的并且以非空结尾,所以你可以在一个循环中遍历它们:
你的编码函数的问题是你永远不会走出递归。希望这对你有帮助:)。
gr8qqesn2#
忘记错误。你的代码都错了。先让它工作起来。(注解掉
HuffmanCodes()
后面的main()
中的内容。)问题是你所有的频率都是零!为什么?因为你用相同的索引来索引两个完全不同的东西,期望它们是相关的。
data[i]
是字符串中出现的第i
个唯一字符。另一方面,freq[i]
是 * 字符 *i
的频率。然后你拉取前七个字节值的频率,0..6,它们都是零。从那以后一切都完了。所以先把它修好。
反正你不需要
data[]
和unique
,只要用freq[]
调用buildHuffmanTree()
,遍历所有freq[]
一次,然后直接构建堆的初始元素,每个元素都是freq[i] != 0
,那么字符就是i
,频率就是freq[i]
。其他意见:
1.您没有检查
malloc()
故障。1.没有
free()
s!1.你两次构建同一棵哈夫曼树。为什么?
1.为什么
MAX_TREE_HT
是100,以及编码和解码字符串的大小?1.正如注解中所指出的,您的
freq[str[i]]++;
需要是freq[str[i] & 0xff]++;
。char
是带符号的,因此您将尝试在没有& 0xff
的情况下访问freq
中大于127的字节的负索引元素。1.您的
encode()
函数没有任何意义。请删除它并重新开始。(您的decode()
函数看起来很好。)提示:遍历哈夫曼树建立一个代码表,然后用它来编码。1.你的"应该是"输出是错误的。忽略它,不管它是你想出的或找到的。