This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Trouble with recursion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trouble with recursion" Watch "Trouble with recursion" New topic

Trouble with recursion

Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
I'm working on creating an expression tree using recursion. It seems to be building the left side ok but whenever I try to setRight the showSub method does not print the < and prints a / meaning that both either left and right are equal to null and that right is equal to null. I am using the prefix expression *+24-34 and this is what the output looks like.

It should look like something this (the format is a little off since I typed in the correct way it should be written):

I believe the problem is in the buildSub method. I figured if the left side is equal to null I can set it to a digit of the string and when the method runs the buildSub method recursively, it would find that something is contained in the left side and will move to the else if statement and setRight to the next digit of the string. Here is the code:
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
I got it accidently, I commented out some of the code for buildSub but I accidently left the recurloop++; in there and it works now.
I agree. Here's the link:
subject: Trouble with recursion
Similar Threads
Help with expression Tree
how to set a node on expression tree
cannot be applied to
getting stackOverFlowError
error: not a statement, '.class' expected, illegal start of expression, ';' expected