关于赫夫曼编码中的代码有点不理解,请高手多指点!
先上代码:(问题在红色字体部分):原码在53课时赫夫曼编码C语言实现由于代码太多我从上面选了一部分复制下来:htTree * buildTree(char *inputString)
{
int * probability = (int *)malloc(sizeof(int)*256);
for(int i=0; i<256; i++)
probability=0;
for(i=0; inputString!='\0'; i++)
probability[(unsigned char) inputString]++;
pQueue * huffmanQueue;
initPQueue(&huffmanQueue);
for(i=0; i<256; i++)
if(probability!=0)
{
htNode *aux = (htNode *)malloc(sizeof(htNode));
aux->left = NULL;
aux->right = NULL;
aux->symbol = (char) i;
addPQueue(&huffmanQueue,aux,probability);
}
free(probability) ; //我调试了程序之后发现这里为什么是每次循环都要释放一次,而不是一次就释放完成!为什么!
while(huffmanQueue->size!=1)
{
int priority = huffmanQueue->first->priority;
priority+=huffmanQueue->first->next->priority;
htNode *left = getPQueue(&huffmanQueue);
htNode *right = getPQueue(&huffmanQueue);
htNode *newNode = (htNode *)malloc(sizeof(htNode));
newNode->left = left;
newNode->right = right;
addPQueue(&huffmanQueue,newNode,priority);
}
htTree *tree = (htTree *) malloc(sizeof(htTree));
tree->root = getPQueue(&huffmanQueue);
return tree;
}
void encode(hlTable *table, char *stringToEncode)
{
hlNode *traversal;
printf("\nEncoding\nInput string : %s\nEncoded string : \n",stringToEncode);
for(int i=0; stringToEncode!='\0'; i++)
{
traversal = table->first;
while(traversal->symbol != stringToEncode)
traversal = traversal->next;
printf("%s",traversal->code);
}
printf("\n");
}
htTree * buildTree(char *inputString)
{
//The array in which we calculate the frequency of the symbols
//Knowing that there are only 256 posibilities of combining 8 bits
//(256 ASCII characters)
int * probability = (int *)malloc(sizeof(int)*256);
//We initialize the array
for(int i=0; i<256; i++)
probability=0;
//We consider the symbol as an array index and we calculate how many times each symbol appears
for(int i=0; inputString!='\0'; i++)
probability[(unsigned char) inputString]++;
//The queue which will hold the tree nodes
pQueue * huffmanQueue;
initPQueue(&huffmanQueue);
//We create nodes for each symbol in the string
for(int i=0; i<256; i++)
if(probability!=0)
{
htNode *aux = (htNode *)malloc(sizeof(htNode));
aux->left = NULL;
aux->right = NULL;
aux->symbol = (char) i;
addPQueue(&huffmanQueue,aux,probability);
}
//We free the array because we don't need it anymore
free(probability);
//We apply the steps described in the article to build the tree
while(huffmanQueue->size!=1)
{
int priority = huffmanQueue->first->priority;
priority+=huffmanQueue->first->next->priority;
htNode *left = getPQueue(&huffmanQueue);
htNode *right = getPQueue(&huffmanQueue);
htNode *newNode = (htNode *)malloc(sizeof(htNode));
newNode->left = left;
newNode->right = right;
addPQueue(&huffmanQueue,newNode,priority);
}
//We create the tree
htTree *tree = (htTree *) malloc(sizeof(htTree));
tree->root = getPQueue(&huffmanQueue);
return tree;
}
调用一次 buildTree() 函数只调用一次 free(probability); 噢,写在中间是因为后边已经不需要它了,所以赶紧释放,免得后边忘记释放导致内存泄漏。
不知道有木有充分理解哥们的问题,如有不对请继续回复。
小甲鱼 发表于 2015-3-15 20:42
调用一次 buildTree() 函数只调用一次 free(probability); 噢,写在中间是因为后边已经不需要它了,所 ...
哦,我又调试了一下。for 偱环里256次每次都要跑到free(probability)代码上去跑一趟,我以为每跑去一次它都释放了一次 小甲鱼 发表于 2015-3-15 20:42
调用一次 buildTree() 函数只调用一次 free(probability); 噢,写在中间是因为后边已经不需要它了,所 ...
补充一点:
for(i=0; i<256; i++)
if(probability!=0)
{
htNode *aux = (htNode *)malloc(sizeof(htNode));
aux->left = NULL;
aux->right = NULL;
aux->symbol = (char) i;
addPQueue(&huffmanQueue,aux,probability);
}//如果这里加一个分号的话,就好理解了它就不会每次都跑下边去free()一次了。我试过了加一个分号的话,这个偱环就不会每次跑下去了。而是等for 偱环执行完了才去执行一次free()
//We free the array because we don't need it anymore
free(probability);
页:
[1]