in reply to Trouble traversing binary tree (was: recursion)

I can't comment on your data structures, because I can't see them. But basic program flow from your example confirms that it will never work properly. You call displaynames with one variable:

$self->displaynames($self->{root});

But displaynames needs two variables to work:

sub displaynames { my $self = shift; $head = shift;

If $head isn't defined, the rest of the routine does not run, and no 'child' nodes are called. Either call displaynames with two arguments, or remove those if statements.

Update: And either you are running without strict or you have defined '$head' as a global, both are bad.

____________________
Jeremy
I didn't believe in evil until I dated it.

Replies are listed 'Best First'.
Re^2: recursion
by Aristotle (Chancellor) on Jun 23, 2002 at 16:56 UTC
    Be very careful - you are wrong. Since he is calling the method using $self->, he implicitly passes $self as the first parameter. Therefor, his call to displaynames() will work as it is. Your update is the real reason, as merlyn++ pointed out.

    Makeshifts last the longest.

      Ack! I'm stupid as charged, except for the bit that should read "real reason, as merlyn++ and jepri-- pointed out". I don't go around copying other peoples nodes without credit.

      ____________________
      Jeremy
      I didn't believe in evil until I dated it.

        Nowhere is there a mention that you copied merlyn - his note was there at the time I replied, so it seemed appropriate to me to mention him.

        :)

        Makeshifts last the longest.