在 java 树中查找 child(递归)

Find child in a java Tree(recursively)

感谢查看。我想要的是获取树,其中 root id 是 param 传递的 id 但它返回空指针。

树设置

TreeIF<DoctorIF> tree = new Tree<DoctorIF>();

   setTree(tree);

   DoctorS ceo = new DoctorS(1, this);
   DoctorS two = new DoctorS(2, this);
   DoctorS three = new DoctorS(3, this);
   DoctorS four = new DoctorS(4, this);
   DoctorS five = new DoctorS(5, this);
   DoctorS six = new DoctorS(6, this);
   DoctorS seven = new DoctorS(7, this);

   TreeIF<DoctorIF> t1 = new Tree<DoctorIF>(three);
   TreeIF<DoctorIF> t2 = new Tree<DoctorIF>(four);
   TreeIF<DoctorIF> t3 = new Tree<DoctorIF>(two);
   tree.setRoot(ceo);

   t1.addChild(new Tree<DoctorIF>(seven));
   t2.addChild(new Tree<DoctorIF>(five));
   t3.addChild(t1);
   t3.addChild(t2);
   t3.addChild(new Tree<DoctorIF>(six));

   tree.addChild(t3);

获取树或树的函数child。我在搜索 id {4,5,6,7} 时得到空指针。

private TreeIF<DoctorIF> getTreeChild(TreeIF<DoctorIF> tree_, int id){
    if (tree_.getRoot().getId()==id && id!=1)return tree_;
    else{
        ListIF<TreeIF<DoctorIF>> list = tree_.getChildren();
        IteratorIF<TreeIF<DoctorIF>> it = list.getIterator();
        while(it.hasNext()){
            TreeIF<DoctorIF> subtree = getTreeChild(it.getNext(), id);
            if (subtree.getRoot()!=null && subtree.getRoot().getId()==id)return subtree;
        }
    }
    return null;
}

 @Override
public DoctorIF getDoctor(int id){
    TreeIF<DoctorIF> tree_ = null;
    tree_ = getTreeChild(tree, id);
    return tree_.getRoot();
}

我添加了一些评论来解释我的更改:

private TreeIF<DoctorIF> getTreeChild(TreeIF<DoctorIF> tree_, int id){
    if (tree_.getRoot().getId()==id && id!=1)return tree_;
    else{
        ListIF<TreeIF<DoctorIF>> list = tree_.getChildren();
        IteratorIF<TreeIF<DoctorIF>> it = list.getIterator();
        while(it.hasNext()){
            TreeIF<DoctorIF> subtree = getTreeChild(it.getNext(), id);
            //subtree might be null at this point
            //In that case, subtree.getRoot() will throw a NullPointerException
            if (subtree == null) continue; //This will prevent the NullPointerException
            if (subtree.getRoot()!=null && subtree.getRoot().getId()==id)return subtree;
        }
    }
    return null;
}

你的功能太复杂了。如果找到,它 return 是子树,否则 null 。所以你不必检查 id 两次。

getTreeChild 可以 return null,但是你在没有检查的情况下取消引用它,所以这可能是空指针异常的来源。

这应该有效:

private TreeIF<DoctorIF> getTreeChild(TreeIF<DoctorIF> tree_, int id){
    if (tree_.getRoot().getId()==id && id!=1)return tree_;
    else{
        ListIF<TreeIF<DoctorIF>> list = tree_.getChildren();
        IteratorIF<TreeIF<DoctorIF>> it = list.getIterator();
        while(it.hasNext()){
            TreeIF<DoctorIF> subtree = getTreeChild(it.getNext(), id);
            if (subtree != null) return subtree; // No need to check the id again.
        }
    }
    return null;
}

你的代码看起来也有点乱,像这样的迭代器。如果您有 ListIF 工具 Iterable<...> and have your IteratorIF implement Iterator<...>.

,则可以使用 java for-each 循环
// ListIF<TreeIF<DoctorIF>> list = tree_.getChildren();
// IteratorIF<TreeIF<DoctorIF>> it = list.getIterator();
// while(it.hasNext()){
//     TreeIF<DoctorIF> subtree = getTreeChild(it.getNext(), id);
//     if (subtree != null) return subtree;
// }

for(TreeIF<DoctorIF> child : tree_.getChildren()) {
    TreeIF<DoctorIF> subtree = getTreeChild(child, id);
    if (subtree != null) return subtree; 
}