I have a tree that I do recursion over the nodes (and then some recursion inside the nodes...). Two problems:
1) Stack level too deep (error message)
2) Speed get really really lousy when the tree is deep. With a small shallow tree the speed is nice. I get that the overall time get long but the handling of each node is slow in a deep tree.
Any way around these issues? I should say that it's running in a Rails environment on Win, Ruby 1.8.2
I have a tree that I do recursion over the nodes (and then some
recursion inside the nodes...). Two problems:
1) Stack level too deep (error message)
2) Speed get really really lousy when the tree is deep. With a small
shallow tree the speed is nice. I get that the overall time get long
but the handling of each node is slow in a deep tree.
Any way around these issues? I should say that it's running in a Rails
environment on Win, Ruby 1.8.2
The easiest change is to use BFS instead of DFS - if that's possible in
your scenario. Alternatives:
- implement a DFS with your own kind of stack
- change the way you store things to avoid such a deep recursion
Also, I guess you made sure that there is no loop, i.e. the structure is
actually a tree...
i now remember that i had a similar problem with a deep recursion and stack
depth. i solved
it by converting the recursive function into a while loop. you can transform
every recursion
into a loop, but it will not be as easy to read anymore.
-- henon
···
On 2/28/06, Robert Klemme <bob.news@gmx.net> wrote:
- change the way you store things to avoid such a deep recursion
On 2/28/06, Robert Klemme <bob.news@gmx.net> wrote:
- change the way you store things to avoid such a deep recursion
[...]
i now remember that i had a similar problem with a deep recursion and
stack depth. i solved
it by converting the recursive function into a while loop. you can
transform every recursion
into a loop, but it will not be as easy to read anymore.
- change the way you store things to avoid such a deep recursion
[...]
i now remember that i had a similar problem with a deep recursion and
stack depth. i solved
it by converting the recursive function into a while loop. you can
transform every recursion
into a loop, but it will not be as easy to read anymore.
That's exactly the other recommendation I gave.
robert
I haven't received the other mail(s) that is talked about. However:
I'm implementing the composite pattern using acts_as_tree and STI in ActiveRecord. The thing that is modeled is nodes in a web site structure that is kind of tree structured (isn't all web sites?). So I try to loop over the nodes to do various operations (generate navigations, publish, distribute etc).
I'm sure that there are ways around this but then you have to code around stuff because of the language implementation and that doesn't feel good at all...
/Marcus
···
On 2/28/06, Robert Klemme <bob.news@gmx.net> wrote:
Well maybe you can take smaller steps to coding around the implementation, first code a tail-recursive version and then convert that into an iterative version.
···
On Feb 28, 2006, at 11:14 AM, marcus wrote:
I'm sure that there are ways around this but then you have to code around stuff because of the language implementation and that doesn't feel good at all...