Creating Huffman Tree with Coding Frequency

In summary, the conversation discusses creating a Huffman tree by finding the frequency of every character in a string and using a struct with left and right pointers. The conversation also mentions using the Day Stout Warren algorithm and creating a sorted linked list for balancing the tree. There is also a suggestion of using a recursive algorithm to update the frequency values of noncharacter nodes.
  • #1
anonim
40
2
Homework Statement
Taking a string, creating the Huffman coding tree and encrypting each character
Relevant Equations
-
First, I get the string and I find the frequency of the every character. My struct is:
C:
typedef struct huffman_tree{
    char c;
    int freq; //unsigned?
    struct huffman_tree *left; // 0 (left)
    struct huffman_tree *right; // 1 (right)
}huffman_tree;

I write this to create Huffman tree:
Code:
huffman_tree *huffman_node_create(char data, unsigned int frequency)
{
    huffman_tree *node = malloc(sizeof(huffman_tree));

        node->data = data;
        node->frequency = frequency;
        node->left = NULL;
        node->right = NULL;

    return node;
}
But I do not know how can I add the frequency the tree, how can I know the number should be right or left?
 
Physics news on Phys.org
  • #2
Review the wiki article, it has an example of a Huffman tree with frequency values shown for each node that should make it obvious How to proceed.

https://en.wikipedia.org/wiki/Huffman_coding

As you add each node to the tree, then you can update the freq values of parent nodes.

Alternatively, you can create the tree in one pass and then walk through it to update the noncharacter nodes frequency values in the next pass. A recursive algorithm would work here.
 
  • #3
Last edited:

1. How does the Huffman tree algorithm work?

The Huffman tree algorithm works by creating a binary tree where the characters with the highest frequency are placed at the top of the tree, and the characters with the lowest frequency are placed at the bottom. This creates a more efficient way of encoding characters, as the most commonly used characters have shorter codes.

2. What is the purpose of creating a Huffman tree?

The purpose of creating a Huffman tree is to compress data and reduce the amount of storage space needed. By assigning shorter codes to more frequently used characters, the overall length of the encoded data is reduced, making it more efficient to store and transmit.

3. How are the coding frequencies determined for each character?

The coding frequencies for each character are determined by analyzing the input data and counting the number of occurrences of each character. The characters with the highest frequency will be assigned shorter codes, while those with lower frequency will be assigned longer codes.

4. Can a Huffman tree be used for any type of data?

Yes, a Huffman tree can be used for any type of data, as long as there are characters with varying frequencies. It is commonly used for compressing text data, but can also be used for images, audio, and other types of data.

5. Are there any drawbacks to using a Huffman tree for data compression?

One potential drawback of using a Huffman tree for data compression is that it requires the entire tree to be transmitted along with the compressed data. This can add extra overhead and may not be practical for very large datasets. Additionally, if the input data changes, the entire tree would need to be recreated, which can be time-consuming.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
Replies
7
Views
894
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
Back
Top