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.

Step Trees
0a:10b:10c:12d:12e:21f:22
1c:12d:12
a:10 b:10
20
e:21f:22
2
a:10 b:10
20
e:21f:22
c:12 d:12
24
3f:22
c:12 d:12
24
a:10 b:10
20
e:21

41

4
a:10 b:10
20
e:21

41

f:22
c:12 d:12
24

46

5
a:10 b:10
20
e:21

41

f:22
c:12 d:12
24

46

87

Which is this tree:

():87 | +--------------+--------+ | | ():41 ():46 +-------+-------+ +-----+--------+ | | | | ():20 e:21 f:22 ():24 +----+----+ +----+----+ | | | | a:10 b:10 c:12 d:12
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 :-)
():87 | +--------------+--------+ | | ():44 ():43 +-------+-------+ +-----+----+ | | | | ():20 ():24 e:21 f:22 +----+----+ +----+----+ | | | | a:10 b:10 c:12 d:12
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:

SymbolWeight D*WHuffmanFixed
a10 30000000
b10 30001001
c12 36110010
d12 36111011
e21 420110
f 22 44 10 11

Yves
--
You are not ready to use symrefs unless you already know why they are bad. -- tadmc (CLPM)


In reply to A Brainteaser -- Huffman with a twist by demerphq
in thread A Golf question maybe? Huffman with a twist by demerphq

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.