Cristoforo has asked for the wisdom of the Perl Monks concerning the following question:
The line that I'm questioning issub basic_tree_find { my ($tree_link, $target, $cmp) = @_; my $node; # $tree_link is the next pointer to be followed. # It will be undef if we reach the bottom of the tree. while ( $node = $$tree_link ) { local $^W = 0; # no warnings, we expect undef values my $relation = ( defined $cmp ? $cmp->( $target, $node->{val} ) : $target <=> $node->{val} ); # If we found it, return the answer. return ($tree_link, $node) if $relation == 0; # Nope - prepare to descend further - decide which way we go. $tree_link = $relation > 0 ? \$node->{left} : \$node->{right}; } # We fell off the bottom, so the element isn't there, but we # tell caller where to create a new element (if desired). return ($tree_link, undef); }
$relation will be greater than 0 (read = 1) if $target is greater than $node->{val}. And this is where I'm confused. If that is the case wouldn't you want to traverse down $node->{right} instead of $node->{left}?
Thanks for any help given here :-)
Chris
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Basic tree - compare question
by dragonchild (Archbishop) on Oct 09, 2007 at 03:06 UTC | |
|
Re: Basic tree - compare question
by GrandFather (Saint) on Oct 09, 2007 at 02:30 UTC | |
|
Re: Basic tree - compare question
by Cristoforo (Curate) on Oct 09, 2007 at 03:31 UTC | |
by dragonchild (Archbishop) on Oct 09, 2007 at 12:08 UTC |