I don't think presorting will ever get you a nice neat code like you said first. It depends on the probablility of the sources.
Anyway, to recurse the tree, you need something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| class Node { Node left; Node right; int code; int codeBits;
void BuildCode( int depth, int currentCode ) { code = currentCode; codeBits = depth;
if(left != null) left.BuildCode(depth+1, code<<1); if(right != null) right.BuildCode(depth+1, (code<<1)+1); } } |
You call this by doing:
1
| rootNode.BuildCode(0,0); |
The root node will get code 0, length 0. Its 'left' will be 0, 1bit, the right '1, 1 bit', and so on down the tree.
Decoding can be done by traversing the tree itself and taking left/right branches as the bits dictate until you reach a 'null' reference. Then the node you are in is the character denoted by the Huffman code.
As an aside, you can create a more efficient tree by using a state machine. I can't remember the code but a bit of googling should find it. Its a bit harder to understand though!
Hope this helps,
- Dom