sub bft {
my ($f, $node) = @_;
my @children = $node->getAllChildren;
return unless @children;
foreach my $child ( @children ) {
$f->( $child->getNodeValue );
}
foreach my $child ( @children ) {
bft($f, $child);
}
}
my $f = sub { print $_[0], "\n" };
# create a tree and populate it, then call bft()
bft($f, $tree);
There are many ways to implement tree structures. Traversals can be somewhat implementation dependent. For trees with a get-children method, breadth-first traversal is pretty straightforward: start with the root, get its children, then loop over the children and recurse. This code was tested using CPAN's Tree::Simple.