The problem is to add a method to this class that reads from a scanner and constructs a tree with the data from the scannerin preorder fashion.
//the class to add the method readTree to
public class IntTree {
private IntTreeNode overallRoot;
...
}
Here is my solution(I have all of the logic down but having trouble with the public private pair)
public void readTree(Scanner s){
overallRoot = readTree(s);
}
private IntTreeNode readTree(Scanner s){
int type = s.nextInt();
IntTreeNode root = new IntTreeNode(s.nextInt());
if(type % 2 == 1){
root.left = readTree(s);
}
if(type==2 || type==3){
root.right= readTree(s);
}
return root;
}
Our style(in general too) is to use a public/private method approach to binary trees. That is take advantage of x=change(x). The return type of the public method should be void(given directions). The return type is IntTreeNode for the private helper because of the returning change(x) part. My question is how would you accompany my set directions of public/private pair, that is write the method signature of the private helper. For my current code, the compiler gives me a duplicate method(i expected that bc the two had the same method signature. I thought about introducing some arbitrary parameter and setting it to null but thought that was bad style. Unlike other problems, you don't need to pass in the int tree node as a parameter bc you don't work with existing data, you're constructing it all from scratch
I think the most straightforward approach would be to name the private method something slightly different, maybe
private IntTreeNode readTreeRecursive(Scanner s)
or
private IntTreeNode readTreeInternal(Scanner s)
is there a reason you can't do something like this?