Hello, I've been working on an assignment that was due last week so right now I'm just trying to understand the concept of how to get this thing to work. I'm supposed to build a tree recursively and at the moment I'm getting an error when I use the build method that looks like this:
The error continues in ExprTree.java:176 more times than I'd like to count or paste into this thread.
Here is the code I currently am using to build the expression tree.
The usual cause of a StackOverflowError is that your code has gone into an infinite recursive loop.
Have another look at the line causing the problem. You're calling buildSub(p) within the buildSub() method - a recursive call. Now, recursion is fine, but you have to be sure that the recursive calls will end at some point. Here, you keep calling buildSub() with the same argument, and nothing else has changed that might mean that the recursion will stop. You call it before the value of i is changed, for example. So once the recursion has started, it will never stop.
Joined: May 25, 2009
It works! Thanks! Well it doesn't really work but it didn't give me the error lol. I only changed where i is incremented.
By the way - I'd recommend changing the name of that variable. i is fine as a variable name when you're talking about a simple local loop. But you're saving the value as part of the object's state, so you really ought to use something more meaningful - something that actually describes what it means.