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:
posted 4 years ago
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.