in reply to Traversing a simple tree
won't save you time immediately, but very handy in the medium & long term.use Graph::Directed; use Graph::Base; use Graph::DFS; my $g = Graph::Directed->new(); growtree($g, $mydatastruct); # create the tree rooted in $g my $search = Graph::DFS->new($g); # for the search... while (my $vertex = $search->next_postorder()){ my $info = $g->get_attribute('myattrib',$vertex); # do stuff with $info... } sub growtree{ my ($g, $mydatastruct) = @_; my $data = # ...get stuff from $mydatastruct; $g->set_attribute('myattrib',$g, $data); my @newedges = # ...get new edges from $mydatastruct $g->add_edge($g,$_) for @newedges; my @newvertices = grep {! $g->has_vertex($_)} @newedges; map { growtree($_,$mydatastruct) } @newvertices; }
|
|---|