- or download this
my $tree = [ 1
, [ 2
...
: $nodefn->( $_, $depth );
}
}
- or download this
sub count_nodes {
my $count;
...
print count_nodes($tree), $/;
# 7
- or download this
sub print_node {
my ($leaf, $depth) = @_;
print " " x ($depth - 1), "* $leaf", $/;
}
- or download this
traverse( \&print_node, $tree );
# * 1
...
# * 5
# * 6
# * 7
- or download this
sub bfs_traverse {
my ($nodefn, $tree, $depth) = @_;
...
bfs_traverse( $nodefn, [map @$_, @branches], $depth )
if @branches;
}
- or download this
bfs_traverse( \&print_node, $tree );
# * 1
...
# * 4
# * 6
# * 5