Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Depending on what you need a tree structure for, you might not need any pre-built implementation. Perl already supports them using arrays of arrayrefs.</p> <p>For example, a simple representation of this tree</p> <pre><code> t / \ a d / \ / \ b c e f </code></pre> <p>could be represented by the following Perl code:</p> <pre><code>$tree = [ t =&gt; [ a =&gt; [ b =&gt; [], c =&gt; [] ] d =&gt; [ e =&gt; [], f =&gt; [] ] ] ]; </code></pre> <p>Here, the tree's representation is as nested pairs: first the element (in this case, the letter), then an anonymous array reference representing the children of that element. Note that <code>=&gt;</code> is just a fancy comma in Perl that exempts you having to put quotes around the the token to the left of the comma, provided it is a single word. The above code could also have been written thus:</p> <pre><code>$tree = [ 't', [ 'a' , [ 'b' , [], 'c' , [] ] 'd' , [ 'e' , [], 'f' , [] ] ] ]; </code></pre> <p>Here's a simple depth-first accumulator of all the elements in the tree:</p> <pre><code>sub elements { my $tree = shift; my @elements; my @queue = @$tree; while (@queue) { my $element = shift @queue; my $children = shift @queue; push @elements, $element; unshift @queue, @$children; } return @elements; } @elements = elements($tree) # qw(t a b c d e f) </code></pre> <p>(For breadth first, change the line <code>unshift @queue, @$children</code> to <code>push @queue, @$children</code>)</p> <p>So, depending on what operations you want to perform on your tree, the simplest thing might be just to use Perl's built-in support for arrays and array references.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload