I have a recursive function to buid up a tree. When the tree grwos too big,
my java give me a error of stack overflow. si there some solutions rather
then reconsidering the algorithms?
Thanks!
> I have a recursive function to buid up a tree. When the tree grwos too big,
> my java give me a error of stack overflow.
Use a better algorithm.
> si there some solutions rather
> then reconsidering the algorithms?
If you really want to waste more memory, read the fine manual:
http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/java.html#nonstandard
/Thomas

Signature
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
hyena - 24 May 2005 15:14 GMT
Thanks!
Seems I have to take a careful look at my iterating alogrithm then.:(
> > I have a recursive function to buid up a tree. When the tree grwos too big,
> > my java give me a error of stack overflow.
[quoted text clipped - 9 lines]
>
> /Thomas
Sigh... *really* helpful Mr Weidenfeller.
If you are getting a stack overflow error, this is NOT because you are
running out of memory. It is because your recursive algorithm does not
properly end its recursion. I would check your logic to see how you
determine when you are done. Typically the pseudocode will be
something like:
Recursive_function(Object current node)
{
// Do pre-recursive stuff here
If (current node is not a leaf node)
{
Recursive_function(current node);
}
// Do post-recursive stuff here
}
mitch - 24 May 2005 19:40 GMT
To overflow the stack you probably have a LOT of recursive calls to
your method. Don't think tens, think thousands or more.
A typical reason, other than a simple programming logic error, is a
data circularity. Suppose you are trying to display a tree of data
elements, where A refers to B and B refers to C and C refers to A.
Each time an element makes a reference you are going one level deeper
in the tree. Guess what? Your program will be drawing a tree that
looks like
A
B
C
A
B
etc. If your tree algorithm is data dependent you have to be careful
to check for circularities in the data, otherwise you'll end up with
an infinite recursion and a stack overflow.
Hope that helps,
- Mitch Gart