I found all solutions so far overly complicated, what am I missing?

please note that the maximal length of the queue is only the sum of children at the current descend path, not all tree elements. (e.g. a binary tree with 10 levels has 2**10 = 2014 nodes but the queue will never exceed 20 = 10*2 elements)

Changing from unshift to push will perform a breadth first search.

use strict; use warnings; use Data::Dump qw/pp dd/; print "\n---------- Input\n"; sub genNested { my $n = shift; map{ rand() < 0.5 ? [ int rand(20), genNested( $n-1 ) ] : int rand +( 20 ) } 1 ..$n; } my @nested = genNested( 3 ); print pp \@nested; print "\n\n---------- my solution\n"; sub genIterator { my $tree = shift; my @queue = @{$tree}; return sub { while ( my ($node) = splice @queue,0,1 ) { # shift is buggy if ( ref $node eq 'ARRAY' ) { unshift @queue, @{$node}; next; } else { return $node } } return; }; } my $iter = genIterator( \@nested ); while ( my ($next) = $iter->() ) { print "$next,\t"; } print "\n\n---------- BUKs goal\n"; sub cbIterator (&$) { my( $code, $tree ) = @_; for my $i ( 0 .. $#$tree ) { if( ref( $tree->[ $i ] ) eq 'ARRAY' ) { &cbIterator( $code, $tree->[ $i ] ); } else { local $_ = $tree->[ $i ]; $code->(); } } } cbIterator { print "$_,\t"; } \@nested;

---------- Input [ [934, [993, 105], [167, [213]]], [71, [120, [973]], 135], [404, [828, [571]], 945], ] ---------- my solution 934, 993, 105, 167, 213, 71, 120, 973, 135, + 404, 828, 571, 945, ---------- BUKs goal 934, 993, 105, 167, 213, 71, 120, 973, 135, + 404, 828, 571, 945,

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

update

fixed issue with strange shift behaviour.

update

changed from shift to splice to handle bug with false nodes


In reply to Re: An iterator for (not "iterating") a recursive data structure. by LanX
in thread An iterator for (not "iterating") a recursive data structure. by BrowserUk

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.