在Java中从给定树检索所有节点

在Java中从给定树检索所有节点,java,algorithm,recursion,tree,avl-tree,Java,Algorithm,Recursion,Tree,Avl Tree,我试图创建一个方法来收集作为参数传递的给定树中的所有节点,但它似乎没有读取任何节点的左分支 到目前为止,我开发的代码如下 private ArrayList<T> collect(AVLTree<T> tree, AVLNode<T> tRoot, ArrayList<T> l) { ArrayList<T> nodes = l; if (tRoot == null) return null;

我试图创建一个方法来收集作为参数传递的给定树中的所有节点,但它似乎没有读取任何节点的左分支

到目前为止,我开发的代码如下

private ArrayList<T> collect(AVLTree<T> tree, AVLNode<T> tRoot, ArrayList<T> l) {

    ArrayList<T> nodes = l;

    if (tRoot == null)
        return null;

    else {
        if (!nodes.contains(tRoot.element())) {
            nodes.add(tRoot.element());

            if (tRoot.getRight() != null) {
                collect(tree, tRoot.getRight(), nodes);
                return nodes;
            }

            else if (tRoot.getLeft() != null) {
                collect(tree, tRoot.getLeft(), nodes);
                return nodes;
            } 
        }
    }

    return nodes;

}
private ArrayList collect(AVLTree tree、AVLNode tRoot、ArrayList l){
ArrayList节点=l;
如果(tRoot==null)
返回null;
否则{
如果(!nodes.contains(tRoot.element())){
nodes.add(tRoot.element());
if(tRoot.getRight()!=null){
收集(树,tRoot.getRight(),节点);
返回节点;
}
else if(tRoot.getLeft()!=null){
收集(树,tRoot.getLeft(),节点);
返回节点;
} 
}
}
返回节点;
}

希望你能在这方面帮我一点忙,因为我现在真的被它卡住了…

两件事让代码无法工作

  • 您只检查任何节点的一个分支,这意味着如果正在检查右分支,则即使左侧有节点,也不会检查左分支
  • 你回来太早了。您不需要在检查每个分支后立即返回。这样做,如果右分支存在,则再次跳过左分支
  • 以下修复将起作用

    private ArrayList<T> collect(AVLTree<T> tree, AVLNode<T> tRoot, ArrayList<T> l) {
    
        ArrayList<T> nodes = l;
    
        if (tRoot == null)
            return null;
    
        else {
            if (!nodes.contains(tRoot.element())) {
                nodes.add(tRoot.element());
    
                if (tRoot.getRight() != null) {
                    collect(tree, tRoot.getRight(), nodes);
                }
    
                if (tRoot.getLeft() != null) {
                    collect(tree, tRoot.getLeft(), nodes);
                } 
            }
        }
    
        return nodes;
    
    }
    
    private ArrayList collect(AVLTree tree、AVLNode tRoot、ArrayList l){
    ArrayList节点=l;
    如果(tRoot==null)
    返回null;
    否则{
    如果(!nodes.contains(tRoot.element())){
    nodes.add(tRoot.element());
    if(tRoot.getRight()!=null){
    收集(树,tRoot.getRight(),节点);
    }
    if(tRoot.getLeft()!=null){
    收集(树,tRoot.getLeft(),节点);
    } 
    }
    }
    返回节点;
    }
    
    编辑:再看一下代码。很少有地方存在代码冗余。可以将其简化和清理为以下内容:

    private ArrayList<T> collect(AVLTree<T> tree, AVLNode<T> tRoot, ArrayList<T> l) {
    
        ArrayList<T> nodes = l;
    
        if (tRoot == null)
            return null;
    
        if (!nodes.contains(tRoot.element())) {
            nodes.add(tRoot.element());
            collect(tree, tRoot.getRight(), nodes); // this is safe since null check exists at top
            collect(tree, tRoot.getLeft(), nodes);
        }
    
        return nodes;
    
    }
    
    private ArrayList collect(AVLTree tree、AVLNode tRoot、ArrayList l){
    ArrayList节点=l;
    如果(tRoot==null)
    返回null;
    如果(!nodes.contains(tRoot.element())){
    nodes.add(tRoot.element());
    collect(tree,tRoot.getRight(),nodes);//这是安全的,因为在顶部存在null检查
    收集(树,tRoot.getLeft(),节点);
    }
    返回节点;
    }
    
    收集
    实际上应该是一种
    无效
    方法。只要在最初调用
    collect
    时传入一个非空列表,该列表最终将包含所有节点,而无需返回任何内容。我同意,这里有更多冗余内容。我不想做一些根本性的改变,比如函数签名的改变。你在这个问题上还有问题吗?@LingZhong都解决了,非常感谢你让我清醒过来!:)