ok, i add some nodes (the addnode routine works, i know), and then call display:
$q = new Tree; $q->addnode(4); $q->addnode(3); $q->addnode(5); $q->addnode(14); $q->addnode(15); $q->addnode(7); $q->addnode(19); $q->display;
so the tree should look like this:
4 / \ 3 5 \ 14 /\ 7 15 \ 19
So i'd expect the display routine to list the numbers in order, starting with the lowest. In fact, what I get is:
3 3
What seems to be happening is that it correctly finds it's starting point, at which point there are 2 levels of recursion. It prints the node value, then goes back to the calling level, but still refers to the lowest level node. So print $head->{Val}, "\n"; still, for some reason prints '3'. Incidentally, if i run it from right to left, i get
19 19 19 19 19
Do you see what i mean? When a subroutine ends and passes control back up to the calling level, is $head not being returned to it's value in the calling routine?

In reply to Re: recursion by Basilides
in thread Trouble traversing binary tree (was: recursion) by Basilides

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.