Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何处理二叉树中的公共/私有对?_Java_Private_Public - Fatal编程技术网

Java 如何处理二叉树中的公共/私有对?

Java 如何处理二叉树中的公共/私有对?,java,private,public,Java,Private,Public,问题是向此类添加一个方法,该方法从扫描仪中读取数据,并以预排序方式使用来自扫描仪的数据构建一个树 //the class to add the method readTree to public class IntTree { private IntTreeNode overallRoot; ... } 这是我的解决方案(我已经记下了所有的逻辑,但是公私对有问题) 我们的风格(通常也是)是对二叉树使用公共/私有方法。也就是说,利用x=变化(x)。公共方法的返回类型应为void(给定的指示)。由

问题是向此类添加一个方法,该方法从扫描仪中读取数据,并以预排序方式使用来自扫描仪的数据构建一个树

//the class to add the method readTree to
public class IntTree {
private IntTreeNode overallRoot;
...
}
这是我的解决方案(我已经记下了所有的逻辑,但是公私对有问题)


我们的风格(通常也是)是对二叉树使用公共/私有方法。也就是说,利用x=变化(x)。公共方法的返回类型应为void(给定的指示)。由于返回的更改(x)部分,私有助手的返回类型为IntTreeNode。我的问题是,你将如何伴随我设置的公共/私有对的方向,即写私人助手的方法签名。对于我当前的代码,编译器给了我一个重复的方法(我原以为bc和bc具有相同的方法签名。我曾想过引入一些任意参数并将其设置为null,但认为这是一种糟糕的风格。与其他问题不同,您不需要将int-tree节点作为参数bc传入。您不使用现有数据,而是从头开始构建它。我认为最重要的是简单的方法可能是将私有方法命名为稍微不同的名称

private IntTreeNode readtreerCursive(扫描仪)

private IntTreeNode readTreeInternal(扫描仪)

有什么原因你不能这样做吗?

我会将第二个方法重命名为其他方法。“populateTree()”也许吧。那么就不可能有重复的方法了。是的,这很有效。我甚至没有想到这种方法
    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;
}