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

o_O:

Your vertices function is printing the number of arguments you gave it: The @_ variable is the list of arguments passed to your subroutine. Since you pass it $root, you'll always get 1 for the result.

You either need to expand the tree into the list of vertices and pass that list to the vertices subroutine, or have the vertices subroutine traverse the tree and keep count.

You haven't shown the code for the XX_order subroutines, but based on the rest of your code, you could simply replace the print statement with an increment to get the vertex count.

Show a bit more of your code and we can give you a little more help.

...roboticus

When your only tool is a hammer, all problems look like your thumb.

  • Comment on Re: program that counts all the vertices in a given tree

Replies are listed 'Best First'.
Re^2: program that counts all the vertices in a given tree
by o_0 (Initiate) on Jul 01, 2013 at 01:42 UTC
    The rest of the program is given in the link above. I don't see a way to delete that.
    I tried doing this instead:
    vertices(pre_order($root))
    but that just gives the output of pre_order($root) again and says the number of vertices is 0.
      "traverse the tree and keep count" - that was the ticket, thanks. By the way, someone mentioned plagiarism. Is it plagiarism if I mentioned the book, and also I modified the code?