I got some code from the Perl Cookbook that generates a random tree and prints it out 3 different ways. I changed it a little, so that the number of vertices would be random. I want the code to count the number of vertices. Some output is below. It says the number of vertices is 1, but how do I get it to count the 5 vertices?
Sample output:
Pre order: 760 75 455 751 839 In order: 75 455 751 760 839 Post order: 751 455 75 839 760 Number of vertices: 1
Code:
#!/usr/bin/perl -w use strict; my($root, $n); # first generate random inserts while ($n++ < int(rand(5))+5) { insert($root, int(rand(1000)))} # now dump out the tree all three ways print "Pre order: "; pre_order($root); print "\n"; print "In order: "; in_order($root); print "\n"; print "Post order: "; post_order($root); print "\n"; vertices($root); exit; ################### sub vertices { my $number=@_; print "Number of vertices: $number"; }

In reply to program that counts all the vertices in a given tree by o_0

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.