Solved

# Build Huffman Tree with Priority Queue in C

Posted on 2007-04-08

Hi,

I am working on the Huffman Algorithm in C for my class project. I am having trouble building the huffman tree to start. This is what I have so far:

TreeNode * buildHuffmanTree(int freqs[256])

{

int i;

PriorityQueue forest;

TreeNode* leftP, *rightP, *tmp;

TreeEntry* top;

CreatePQueue(&forest);

/* YOU FILL IN THE REST OF THIS CODE */

for (i = 0; i < 256; i++)

{

if (freqs[i] > 0)

{

leftP = malloc (sizeof(TreeNode));

leftP->left = NULL;

leftP->right = NULL;

pqInsert(leftP, &forest);

}

}

while (freqs[i] > 1)

{

leftP = deleteMin(&forest);

rightP = deleteMin(&forest);

tmp->left = leftP;

tmp->right = rightP;

tmp = MakeNode(top->symbol, top->weight, tmp->left, tmp->right);

pqInsert(tmp, &forest);

}

return tmp;

{

If anyone has any advice I would appreciate it. Thanks.