• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trouble with recursion

 
Dustin Schreader
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic