You would need the two methods to be defined something like:
sub FIRSTNODE { my ($root_node) = @_; my $node = $root_node; while ($node->{left}) { $node = $node->{left}; } return $node; } sub NEXTNODE { my ($node) = @_; if ($node->{right}) { $node = FIRSTNODE($node->{right}); } elsif ($node->{parent}) { if ($node->{parent}->{left} == $node) { $node = $node->{parent}; } else { while ($node->{parent}->{right} == $node) { $node = $node->{parent}; } return unless $node; $node = $node->{parent}; } } else { $node = undef; } return $node; }
The NEXTNODE method there traverses the tree, looking for the next logical node. This assumes that your B-Tree implementation uses a hash-style tree with references between nodes. To switch it to using text keys instead, you could write wrapper methods, as proposed below, or modify the code slightly to set $key instead of $node.

The FIRSTKEY/NEXTKEY methods would merely return the key instead of the node reference, something like:
sub FIRSTKEY { my $node = FIRSTNODE($root_node); return $node && $node->{key}; } sub NEXTKEY { my ($key) = @_; my $node = NEXTNODE (FindNode ($key)); return $node && $node->{key}; }

In reply to Re: Serializing Trees using FIRSTKEY/NEXTKEY methods? by tadman
in thread Serializing Trees using FIRSTKEY/NEXTKEY methods? by rrwo

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.