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:

# the nested array refs my $tree = [ 'Root', [ 'Child1', [ 'GrandChild1', 'GrandChild2' ], 'Child2' ] ]; # the nested hash references my $tree = { 'Root' => { 'Child1' => { 'GrandChild1' => {}, 'GrandChild2' => {} }, 'Child2' => {} } };
And the more complex relational approach:
# 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' } ];
Now for my reasoning.

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,

-stvn

In reply to How do "you" make a tree in perl by stvn

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.