Permalink
Browse files

handle breadth-first search properly now, can do it with a loop no ne…

…ed to make recursive function
  • Loading branch information...
1 parent a0a90b5 commit 3d1882a78c1c039461173b5b224d67d41e307f31 @hyphaltip hyphaltip committed Apr 26, 2012
Showing with 20 additions and 12 deletions.
  1. +20 −12 Bio/Tree/Tree.pm
View
@@ -191,29 +191,37 @@ Function: Return list of Bio::Tree::NodeI objects
Returns : array of Bio::Tree::NodeI objects
Args : (named values) hash with one value
order => 'b|breadth' first order or 'd|depth' first order
+ sortby => $sortby [optional] "height", "creation", "alpha", "revalpha",
+ or coderef to be used to sort the order of children nodes. See L<Bio::Tree::Node> for details
=cut
sub get_nodes{
my ($self, @args) = @_;
-
+ warn("args are @args\n");
my ($order, $sortby) = $self->_rearrange([qw(ORDER SORTBY)],@args);
$order ||= 'depth';
$sortby ||= 'none';
- my $node = $self->get_root_node || return;
- if ($order =~ m/^b|(breadth)$/oi) {
- my @children = ($node);
- for (@children) {
- push @children, $_->each_Descendent($sortby);
+ my @children;
+ my $node = $self->get_root_node || return undef;
+
+ if ($order =~ m/^b$/oi) {
+ @children = ($node);
+ my @to_process = ($node);
+ while( @to_process ) {
+ my $n = shift @to_process;
+ my @c = $n->each_Descendent($sortby);
+ push @children, @c;
+ push @to_process, @c;
}
- return @children;
- }
-
- if ($order =~ m/^d|(depth)$/oi) {
+ } elsif ($order =~ m/^d$/oi) {
# this is depth-first search I believe
- my @children = ($node,$node->get_all_Descendents($sortby));
- return @children;
+ @children = ($node,$node->get_all_Descendents($sortby));
+ } else {
+ $self->verbose(1);
+ $self->warn("specified an order '$order' which I don't understan\n");
}
+ @children;
}
=head2 get_root_node

0 comments on commit 3d1882a

Please sign in to comment.