Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
why aren't these two equivalent?
sub traversal { my $self = shift; my @nodes; # self is FIRST @nodes = ( $self ); push @nodes, map { $_->traversal( ) } @{ $self->{children} }; return @nodes;
sub traversal { my $self = shift; my @nodes; # self is FIRST @nodes = ( $self ); foreach my $node ( @{ $self->{children} } ) { push @nodes, $node; $node->traversal(); } return @nodes;
the second one correctly handles "width", but fails "height". (width is multiple nodes in the children array and no child node has any children) (height is a single node in the children array and each node has one child)
it visits every node; but it pushes just the first two nodes into the array. what am i missing?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: map & arrays & recursion
by Corion (Patriarch) on Sep 30, 2010 at 21:39 UTC | |
|
Re: map & arrays & recursion
by kennethk (Abbot) on Sep 30, 2010 at 22:07 UTC | |
by ikegami (Patriarch) on Oct 01, 2010 at 03:13 UTC | |
by Anonymous Monk on Oct 02, 2010 at 00:06 UTC | |
by ikegami (Patriarch) on Oct 02, 2010 at 04:21 UTC |