<?xml version="1.0" encoding="windows-1252"?>
<node id="74011" title="Re (tilly) 3: Golf: Tree searching" created="2001-04-19 21:55:34" updated="2005-07-29 12:56:34">
<type id="11">
note</type>
<author id="26179">
tilly</author>
<data>
<field name="doctext">
As [koolade] [id://73933|pointed out], not all of these
work.  Yours in particular goes into deep recursion.
Works in theory is not good enough, in golf you are going
to learn a lot about the assumptions you cannot make.
And yours has at least 2 separate (and serious) mistakes.&lt;P&gt;

BTW the counts as I list them are just for the body of the
sub, so if yours did work it would be 51 characters.&lt;P&gt;

Playing independently I came in with several solutions
that are small, and the following which is similar to
yours really does work at 58:
&lt;code&gt;
sub f {
my($s,$t)=@_;$t?$$t{d}eq$s?$t:f($s,$$t{l})||f($s,$$t{r}):0
}
&lt;/code&gt;
And here, thanks to [koolade], is the test that I use:
&lt;code&gt;
$t = {
    d =&gt; 'd',
    l =&gt; {
        d =&gt; 'b',
        l =&gt; { d =&gt; 'a', l =&gt; 0, r =&gt; 0, },
        r =&gt; { d =&gt; 'c', l =&gt; 0, r =&gt; 0, },
    },
    r =&gt; {
        d =&gt; 'f',
        l =&gt; { d =&gt; 'e', l =&gt; 0, r =&gt; 0, },
        r =&gt; { d =&gt; 'g', l =&gt; 0, r =&gt; 0, },
    }
};

sub test {
  my $val = f(@_);
  print $val ? "$val-&gt;{d}:$val\n" : "$val\n";
}

test('e',$t);
test('O',$t);
&lt;/code&gt;</field>
<field name="root_node">
73900</field>
<field name="parent_node">
73921</field>
</data>
</node>
