why not generalize your problem slightly and use Jarko Heitaniemi's Graph packages to do the work of the postorder traversal for you?

i am normally reluctant to suggest any reengineering of code presented, but i have personally saved so much time using Graph::Base, Graph::Directed and Graph::DFS that i feel compelled to sketch out the following:

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; }
won't save you time immediately, but very handy in the medium & long term.

hope it helps;

...wufnik

-- in the world of the mules there are no rules --

In reply to Re: Traversing a simple tree - using GRAPH::DFS by wufnik
in thread Traversing a simple tree by nightwatch

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.