sub 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); }