If you want to be "tricky", you can pack a binary heap into a one-dimensional list:

my $heap = ['Root', 'Child1', 'Child2', 'Grandchild1', Grandchild2'];

See heapsort for a stereotypical use of binary heaps. They're not as general as n-ary trees (being strictly binary and left-balanced), but they might make an interesting example.

For further fun and games with low-level-hacking data structures, pack a binheap into a string, with either null-separated or fixed-width fields:

# "\0" is a null, right? my $ns_heap = "R\0C1\0C2\0GC1\0GC2\0"; my $fw_heap = "R\0\0C1\0C2\0GC1GC2";
Again, this is almost gratuitously ugly, but might make a "fun" example.

--
F o x t r o t U n i f o r m
Found a typo in this node? /msg me
% man 3 strfry


In reply to Re: How do "you" make a tree in perl by FoxtrotUniform
in thread 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.