in reply to (Golf) Building a Better Binary Tree


Here's my go,

It's strict and warnings compliant. Not a well-adjusted tree, but it's more adjusted than the previous entries:

#234567890123456789012345678901234567890123456789012345678 $a=shift;@_=sort$a @_;my%a=(pop,[0,0]);$_=$#_;%a=$_>0?($_[ $_-1],[0,0],$_[$_--],{%a}):($_[$_],{%a}),$_--while$_>-1;%a
116 characters. Hmm, this isn't nearly as short as it was when it didn't do anything correctly ;-)

jynx

update: fixed code to work for base case and sort, modified post to reflect change.

Replies are listed 'Best First'.
Re: Re: (Golf) Building a Better Binary Tree
by dragonchild (Archbishop) on Oct 09, 2001 at 16:43 UTC
    Doesn't every key in the binary tree have to be unique? Yours doesn't do that, given data of
    ( 2, 1, 3, 4, 7, 6, 5, 4, 3, 2, 7, 9, 5 )

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.


      i may be wrong but,

      The problem description says that the keys may or may not be distinct. It doesn't say we have to flatten the list to unique keys only. If i'm missing something please tell me, but i don't see where it says we must make the keys unique...

      jynx