Re: (Golf) Building a Better Binary Treeby trantor (Chaplain)
|on Oct 08, 2001 at 19:53 UTC||Need Help??|
Just to get things started...
However I'm definitely not proud of this code. Whitespace has not been taken out for clarity.
The function basically outputs the worst possible tree after sorting the array, each left leaf is always 0 and the right is built iteratively, thus not using recursion. Since building strings is much easier than building trees (no pun intended), I did exactly that, evalling the result. It is a hash, not a reference, as the example code seems to suggest. Outputting a ref would allow some more savings :-)
Warning: for $func to work
properly with sort, it should be prototyped:
And now the code:
It should be 103 chars excluding function declaration and non significative whitespace.
It can be tested in this context:
Update: In the initial post I forgot to add the character count :-)
Update 2: After tilly's comment I changed the function so that there's no need to prototypes, and I took some time to compress it further down to 88 chars: