As I was working on a paper on data structures for trees I came across the problem of explaining various tree traversal types. In the attached code I (am hoping to..) demonstrate:
#!/usr/bin/perl use strict; use warnings; use Bio::Phylo::Parsers; # the single quoted string is a parenthetical # tree description in "Newick" format. This # format can be visualized using, among other # programs, TreeView: # http://taxonomy.zoology.gla.ac.uk/rod/rod.html my $treestring = '(((A,B)n1,C)n2,D)n3;'; my $parser = new Bio::Phylo::Parsers; my $tree = $parser->parse( -format => 'newick', -string => $treestring )->first; my $root = $tree->get_root; print qq{###########PRE-ORDER########\n}; &pre_order($root); print qq{###########IN-ORDER#########\n}; &in_order($root); print qq{##########POST-ORDER########\n}; &post_order($root); print qq{##########DEPTH FIRST#######\n}; &depth_first($root); print qq{#########BREADTH FIRST######\n}; &breadth_first($root); sub pre_order { my $node = shift; print $node->get_name, "\n"; my $fd = $node->get_first_daughter; my $ld = $node->get_last_daughter; &pre_order($fd) if $fd; &pre_order($ld) if $ld; } sub in_order { my $node = shift; my $fd = $node->get_first_daughter; my $ld = $node->get_last_daughter; &in_order($fd) if $fd; print $node->get_name, "\n"; &in_order($ld) if $ld; } sub post_order { my $node = shift; my $fd = $node->get_first_daughter; my $ld = $node->get_last_daughter; &post_order($fd) if $fd; &post_order($ld) if $ld; print $node->get_name, "\n"; } sub depth_first { my $node = shift; print $node->get_name, "\n"; my $fd = $node->get_first_daughter; my $ns = $node->get_next_sister; if ( $fd ) { &depth_first($fd); } if ( $ns ) { &depth_first($ns); } } sub breadth_first { my $node = shift; print $node->get_name, "\n"; my $fd = $node->get_first_daughter; my $ns = $node->get_next_sister; if ( $ns ) { &breadth_first($ns); } if ( $fd ) { &breadth_first($fd); } }

In reply to tree traversal types by rvosa

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.