in reply to Challenge: Perl 5: lazy sameFringe()?
Laurent_R above is looking for a recursive solution. Recursion is the easiest way to traverse a tree. My idea to do this is sketched like this for simultaneous recursive traversal:
use strict; use warnings; sub recursiveFringe { for( @{ +shift } ) { if( ref $_ ) { recursiveFringe( $_ ) } else { print "$_\n" }; } } sub sameFringe { # create a subthread for each tree executing the above recursiveFrin +ge # in such a way that the output from print above can be read in # while(<...>) in the main thread. # once a difference occurs return FALSE and terminate threads } my @trees = ( [ [ 1, [ [2, [4, 7] ], 5 ] ], [3, [6, [8, 9] ] ] ], [ [ 1, [ [2, [4, 7] ], 5 ] ], [3, [6, [8, 9] ] ] ], [ [ 1, [ [ [2, 4], 7], 5 ] ], [3, [6, [8, 9] ] ] ], [ [ 1, [ [2, [4, 7] ], 5 ] ], [3, [6, [8, [9, 0 ] ] ] ] ] +, [ [ 1, [ [0, [4, 7] ], 5 ] ], [3, [6, [8, 9] ] ] ], ); print sameFringe( $trees[0], $_ )?"Same\n":"Different\n" for @trees;
As I have no experience with threads, I do not really know how to start this or whether it is even feasible. What do you think?
LanX: As far as I can see, this would work for trees with more branches than two...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Challenge: Perl 5: lazy sameFringe()?
by BrowserUk (Patriarch) on Jun 30, 2013 at 13:54 UTC |