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:
void BuildCode( int depth, int currentCode )
code = currentCode;
codeBits = depth;
if(left != null)
if(right != null)
You call this by doing:
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,