Don't ask to ask, just ask  
PerlMonks 
A Brainteaser  Huffman with a twistby demerphq (Chancellor) 
on Sep 05, 2001 at 02:52 UTC ( #110191=note: print w/replies, xml )  Need Help?? 
Hi Tilly, Ok from the example you provided (which I converted into a funky table on a bizarre whim!) we have the tree as constructed directly by huffman encoding. The idea of this encoding as you know is to have each leaf at such a position that minimizes the overall depth x weight of the tree. We want to convert that tree into an equivelent in those terms but with the additional constraint that they are ordered left to right by weight. The fact that a huffman tree is constructed so this branch goes left and that branch right is completely spurious so long as the relative weighting between the nodes is maintained. Also the problem is not to generate a huffman tree. You get that as input. The problem is to find its equivelent tree. It has occurred to me that this not really golf but rather a brainteaser and for that maybe I should change the title. Also I will post my solution if you think I should.
Which is this tree:
It is obvious that the above tree can be easily transformed into the below tree which satisfies all of the properties of the huffman encoding. (Isomere or Isomorph comes to mind :) In other words, the two trees are identical in terms of the purpose of huffman encoding, ie the minimal paths the leafs according to the weights of the leafs. The following list maps out the paths of the two trees:
Yves
In Section
Meditations

