There are many ways to build tree-like data-structures in perl, and I am currently trying to build a list of them (specifically n-ary trees and without using objects or modules (more about this reasoing later on)). I am looking for examples from other monks on how they create trees, or have created trees in the past or even of tree structures they know that common modules require/return. I currently have the following examples.
The straight forward recursive data-structure approach:
And the more complex relational approach:# the nested array refs my $tree = [ 'Root', [ 'Child1', [ 'GrandChild1', 'GrandChild2' ], 'Child2' ] ]; # the nested hash references my $tree = { 'Root' => { 'Child1' => { 'GrandChild1' => {}, 'GrandChild2' => {} }, 'Child2' => {} } };
Now for my reasoning.# using arrays where # the array index is # the id refered to by # the parent_id my $tree = [ # parent_id, node [ undef, 'Root' ], [ 0, 'Child1' ], [ 0, 'Child2' ], [ 1, 'GrandChild1' ], [ 1, 'GrandChild2' ] ]; # using hashes in a similar manner my $tree = [ { id => 0, parent_id => undef, node => 'Root' }, { id => 1, parent_id => 0, node => 'Child1' }, { id => 2, parent_id => 0, node => 'Child2' }, { id => 3, parent_id => 1, node => 'GrandChild1' }, { id => 4, parent_id => 1, node => 'GrandChild2' } ];
I am the author of a module called Tree::Simple which is a basic object-oriented n-ary tree. I have also created a small set of Visitor classes for use with this module at Tree::Simple::VisitorFactory which right now it's mostly pretty basic stuff. However, I am in the process of getting ready to release the next version of Tree::Simple::VisitorFactory which has about twice as many Visitors in it (hopefully many useful ones). I am currently putting together a set of Visitors to do conversions (both to and from) other tree representations, so I am looking for examples from as many people as possible to be sure I cover as many styles of trees in perl as possible. Any help is very much appreciated.
Thanks in advance,
In reply to How do "you" make a tree in perl by stvn
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |