<p>I'm writing a Perl script and would like to use a n-ary tree data structure.</p>

<p>Is there a good implementation that is available as source code (rather than part of a Perl library) ?</p>

[{'answer_id': 37717, 'author': 'Matthew Watson', 'author_id': 3839, 'author_profile': 'https://Stackoverflow.com/users/3839', 'pm_score': 2, 'selected': False, 'text': '<p>I don\'t really understand why you want it was "source" rather than as a perl library, but you can download the source for any CPAN module. </p>\n\n<p>I haven\'t used it, but <a href="http://search.cpan.org/~rkinyon/Tree-1.01/lib/Tree.pm" rel="nofollow noreferrer">Tree</a> looks to fill your requirements.</p>\n'}, {'answer_id': 38278, 'author': 'ChrisN', 'author_id': 3853, 'author_profile': 'https://Stackoverflow.com/users/3853', 'pm_score': 4, 'selected': True, 'text': '<p>Adding to what Matthew already said, it looks like the following modules would be suitable:</p>\n\n<p><a href="http://search.cpan.org/perldoc?Tree::Nary" rel="nofollow noreferrer">Tree::Nary</a><br>\n<a href="http://search.cpan.org/perldoc?Tree::Simple" rel="nofollow noreferrer">Tree::Simple</a><br>\n<a href="http://search.cpan.org/perldoc?Tree" rel="nofollow noreferrer">Tree</a></p>\n'}, {'answer_id': 38857, 'author': 'nohat', 'author_id': 3101, 'author_profile': 'https://Stackoverflow.com/users/3101', 'pm_score': 2, 'selected': False, '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>\n<p>For example, a simple representation of this tree</p>\n<pre><code>             t\n           /   \\\n          a     d\n         / \\   / \\\n        b   c e   f\n</code></pre>\n<p>could be represented by the following Perl code:</p>\n<pre><code>$tree = [ t =&gt; [ a =&gt; [ b =&gt; [], c =&gt; [] ]\n                 d =&gt; [ e =&gt; [], f =&gt; [] ] ] ];\n</code></pre>\n<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 token to the left of the comma, provided it is a single word. The above code could also have been written thus:</p>\n<pre><code>$tree = [ 't', [ 'a' , [ 'b' , [], 'c' , [] ]\n                 'd' , [ 'e' , [], 'f' , [] ] ] ];\n</code></pre>\n<p>Here's a simple depth-first accumulator of all the elements in the tree:</p>\n<pre><code>sub elements {\n    my $tree = shift;\n\n    my @elements;\n    my @queue = @$tree;\n    while (@queue) {\n        my $element  = shift @queue;\n        my $children = shift @queue;\n        push @elements, $element;\n        unshift @queue, @$children;\n    }\n\n    return @elements;\n}\n\n@elements = elements($tree)     # qw(t a b c d e f)\n</code></pre>\n<p>(For breadth first, change the line <code>unshift @queue, @$children</code> to <code>push @queue, @$children</code>)</p>\n<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>\n"}]
['https://Stackoverflow.com/questions/37662', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/381/']