简体   繁体   中英

Using Java, how do I get the encoded tree of a Huffman Tree?

Given a constructed a tree and all the variables needed for it to work, I simply need to understand HOW one goes about getting an encoded tree from a Huffman Tree.

More specifically, I need to return the String Encoding of a Huffman Tree. No parameters are passed to the function. It is simply a getHuffmanTreeEncoded() function that returns the encoded string of the Tree and I am not sure how I would go to that.

I don't provide code for this question because the rest of it is already done and it is very long / for school... I think I would better understand with words.

Do I need to traverse the tree? Do I need to recursively loop? I need to pass a string variable with the Huffman Tree string encoded. How would I go about that, assuming functions and structure for everything already exists (the tree, getting encoded tests, priority queue implementation ,etc.). What general steps does one take to get the string of a Huffman tree?

-Thanks

What I think you're asking is: If I have symbol, how do I use the Huffman tree to generate the code for that symbol.

You don't. That's a terrible thing to have to do for every single symbol in your message. What you would do instead is traverse the entire tree recursively once , and at each leaf, add the symbol and its code to a table. Discard the tree and use the table to do your encoding.

You don't even need to do that, because you can ignore the actual code built in to the assignments of 0's and 1's to left and right branches. However that assignment is entirely arbitrary, where any other assignment will give you the same compression. The only information you need from the Huffman tree is the code length , in bits, for each symbol. Traverse the tree recursively and generate a table of the symbols and their code lengths.

Then you can use a canonical Huffman code for encoding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM