I made the Huffman code algorithm... and made the tree as it should be made..
The problem comes how do I print the code from the tree.. Theoretically I know the code generation.. But how to implement in C++..
Using TC.

The following tree is constructed for this data

Alphabet  : a  | b  | c  | d  | e | f 
Frequency : 45 | 13 | 12 | 16 | 9 | 5


                         100  <----- root
                         /  \
                        /    \
                     a,45    55
                            /  \
                           /    \
                         25     30
                        /  \    | \
                       /    \   |  \
                    c,12  b,13  14  d,16
                                /\
                               /  \
                             f,5   e,9

Thanks in Advance!!

Recommended Answers

All 2 Replies

I guess you need an algorithm to convert a tree node to an (x,y) coordinate based on the overall tree. The y is easy. That's just the depth of the node. The x is going to be harder. And what's really going to be hard is making sure everything doesn't bump int each other and overlap. Even harder will be the / and \ symbols. They might not end up at the right angles. It's the closest thing you have to "drawing" a line, but it ain't that good. Might want to pick a or asterisk instead..

Then you have to turn the (x,y) elements into cout statements. So get out the graph paper and start making an algorithm on how to convert. The math isn't all that hard, just algebra, but you have to break it down into extreme detail and devise a formula. Remember, the program has no judgment. It won't say to itself, "Whoops, this bumped into something, I better draw it here instead." You have to anticipate every possibility to make your formula work. It's a math problem more than anything.

I guess you need an algorithm to convert a tree node to an (x,y) coordinate based on the overall tree. The y is easy. That's just the depth of the node. The x is going to be harder. And what's really going to be hard is making sure everything doesn't bump int each other and overlap. Even harder will be the / and \ symbols. They might not end up at the right angles. It's the closest thing you have to "drawing" a line, but it ain't that good. Might want to pick a or asterisk instead..

Then you have to turn the (x,y) elements into cout statements. So get out the graph paper and start making an algorithm on how to convert. The math isn't all that hard, just algebra, but you have to break it down into extreme detail and devise a formula. Remember, the program has no judgment. It won't say to itself, "Whoops, this bumped into something, I better draw it here instead." You have to anticipate every possibility to make your formula work. It's a math problem more than anything.

The tree i made in the post was for reference only.. I was asking the code generation (001,100, etc...)
Anyways I did it... Thanks..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.