aspose file tools*
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trouble with recursion" Watch "Trouble with recursion" New topic
Author

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: http://aspose.com/file-tools
 
subject: Trouble with recursion