We don't bite newbies here... much | |
PerlMonks |
Re: Eliminating Recursive Tree Walking by using MJD-style Infinite Streams?by jethro (Monsignor) |
on Jun 16, 2008 at 22:06 UTC ( [id://692391]=note: print w/replies, xml ) | Need Help?? |
Are you sure that your recursion gets so deep that you run out of stack space. How many paths are in your database? Since you seem to do depth first search and if your tree isn't degenerated into one long path then you would need gazillions of paths to exceed the stack space But since you collect all found paths in memory (in @current), that array will get big pretty fast. So instead you should drop @current and print or save to file your found paths on success (inside your unless (defined $branches) ... and if (exists $regs... control structures. For this you need another parameter to your follow_paths subroutine that gives the momentary path to this point to the subroutine So keep your recursive routine, it is much more readable, just save the results, if you can't keep them in memory. But there is another possibility: Are you sure your data hasn't any loops, i.e. 0,1,0 points to 3,0,0 which points to 0,1,0 and both points are not in regs ? If there are loops you might put the points of your momentary path into a hash and call foul when you are at a point that already is in that hash.
In Section
Seekers of Perl Wisdom
|
|