Consider the binary tree developed by Moose :: Cookbook :: Basics :: Recipe3
To get all the nodes in the preorder , I could add the following routine to the BinaryTree package
sub pre_order {
my ($self,$aref) = @_;
push @$aref, $self->node;
pre_order($self->left,$aref) if $self->has_left;
pre_order($self->right,$aref) if $self->has_right;
}
The subitem should be used as follows:
my $btree = BinaryTree->new;
my @nodes_in_preorder;
$btree->pre_order(\@nodes_in_preorder);
How can I change the routine to use the syntax as shown below:
my @nodes_in_preorder = $btree->pre_order();
to be able to do things like
for ($btree->pre_order()) {
later.
Does that make sense, or am I pedantic?
source
share