nightwatch has asked for the wisdom of the Perl Monks concerning the following question:
I have a tree that consists of several joined nodes, which are implemented as blessed hash references. Each node knows about its first child, its immediate parent, and its first sibling ({child_n}, {parent_n}, and {sibling_n} respectively). It's been pointed out to me that instead of using a singly-linked list for siblings I could have used an array ref, but I don't think that'll matter much, since order is important and so I only go through one way.
What I was wondering about is the best and most elegant way to do a postorder traversal of the tree. None of the solutions I've come up with are very elegant - I've used recursive _private_methods, recursive subroutine references, and a stack with a while { ... } continue { ... } loop, but nothing turns out very readable or convenient. Is there a better way?
Thanks in advance!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Traversing a simple tree
by tachyon (Chancellor) on Jun 15, 2004 at 20:52 UTC | |
|
Re: Traversing a simple tree
by hardburn (Abbot) on Jun 15, 2004 at 20:27 UTC | |
|
Re: Traversing a simple tree - using GRAPH::DFS
by wufnik (Friar) on Jun 17, 2004 at 13:18 UTC |