Java 函数返回树的叶子

Java 函数返回树的叶子,java,arrays,tree,Java,Arrays,Tree,我有树形结构: public class Tree{ int topSize = 10 ; Tree[] children2 = new Tree[topSize]; Tree parent; String data; int i = 0; public void addChild(Tree child) { child.setParent(this); this.children2[i++] = child; } } treeArray

我有树形结构:

public class Tree{
  int topSize = 10 ;
  Tree[] children2 = new Tree[topSize];
  Tree parent;
  String data;
  int i = 0;
   public void addChild(Tree child) {
      child.setParent(this);
      this.children2[i++] = child;
   }
}
treeArray[0]
data="d",                          children[] -    children[3]->data="c",children[0]->data="a", children[0]=null                                                                                                   
                                    /     \       
children[0]->data="b",children[0]=null    children[1]->data="b",children[0]=null
我在我的程序中使用它,例如:

example 1:                           example 2:
          F                             H
         /                             / \
        E                             I   J
       /                             /
      D                             T
     /
    C
treeArray[0]
data="d",                          children[] -    children[3]->data="c",children[0]->data="a", children[0]=null                                                                                                   
                                    /     \       
children[0]->data="b",children[0]=null    children[1]->data="b",children[0]=null
我想要一个结果为我提供叶子的函数:
示例1:函数应返回一个数组,其中包含:C(树节点)
示例2:函数返回数组[0]=T(在数据T中包含的树节点)中的me结果,数组[1]=J(树节点)。

treeArray[0]
data="d",                          children[] -    children[3]->data="c",children[0]->data="a", children[0]=null                                                                                                   
                                    /     \       
children[0]->data="b",children[0]=null    children[1]->data="b",children[0]=null
我需要将示例1和示例2中的树展平到:

example 1         example 2
solution          solution
    F                 H
 /  |  \            / | \
E   D  C           I  J  T
treeArray[0]
data="d",                          children[] -    children[3]->data="c",children[0]->data="a", children[0]=null                                                                                                   
                                    /     \       
children[0]->data="b",children[0]=null    children[1]->data="b",children[0]=null
我有以下函数,它接受一个叶作为参数:

public static Tree find(Tree edge){
    if(edge != edge.parent){
        edge.parent = find(edge.parent);
        edge.parent.addChild(edge);
    }
    return edge.parent;
}
treeArray[0]
data="d",                          children[] -    children[3]->data="c",children[0]->data="a", children[0]=null                                                                                                   
                                    /     \       
children[0]->data="b",children[0]=null    children[1]->data="b",children[0]=null
因此,在示例1中,我使用:find(C)
例如,我认为我应该使用:find(T),然后find(edge J)
我主要给出包含以下内容的数组:

treeArray[0]
data="d",                          children[] -    children[3]->data="c",children[0]->data="a", children[0]=null                                                                                                   
                                    /     \       
children[0]->data="b",children[0]=null    children[1]->data="b",children[0]=null

我有同样的孩子,但后来我删除了它,所以我想你在追求一种类似于以下的方法:

treeArray[0]
data="d",                          children[] -    children[3]->data="c",children[0]->data="a", children[0]=null                                                                                                   
                                    /     \       
children[0]->data="b",children[0]=null    children[1]->data="b",children[0]=null
  public static void findLeaves(Tree root, List<Tree> leaves){

        //iterate over children 
        for(Tree child : root.children2) { //better use getter 

            //if child has no children it is a leaf. Add it to list 
            if(! hasChildren(child))  leaves.add(child);
            //if child has children, check them 
            else  findLeaves(child, leaves);
        }

        return ;
  } 

  static boolean hasChildren(Tree child) {

      for(int i=0; i < child.children2.length; i++) {

          if(child.children2[i] != null) return true;
      }
      return false;
  }
publicstaticvoidfindleaves(树根、列表叶){
//反复浏览儿童
对于(Tree-child:root.children2){//最好使用getter
//若子项并没有子项,那个么它是一个叶子。将它添加到列表中
如果(!hasChildren(child))离开,则添加(child);
//如果孩子有孩子,检查他们
否则会留下(孩子、树叶);
}
返回;
} 
静态布尔hasChildren(树状子对象){
for(int i=0;i
该方法应将所有叶(没有子节点的节点)添加到
列表叶
(如果需要,可以将其转换为数组)。
通过以下方式使用它:

treeArray[0]
data="d",                          children[] -    children[3]->data="c",children[0]->data="a", children[0]=null                                                                                                   
                                    /     \       
children[0]->data="b",children[0]=null    children[1]->data="b",children[0]=null
List<Tree> leaves = new ArrayList<>();//a list to hold leaves after method runs
findLeaves(root, leaves);
List leaves=new ArrayList()//在方法运行后保存叶子的列表
叶片(根、叶);
请注意:
1.我无法运行并检查该方法。请仔细检查。

2.该方法不是空安全的。
都不应为空

例如,我在我的帖子中添加了数组的外观。您的函数给了我一个错误:if(child.children2.length==0)leaves.add(root);,我调试它,但这不起作用,因为我总是使用大小为10的数组,例如,我稍后在main中给出这个值,但我必须在我的结构中使用数组,所以总是children2的长度为10,当root没有子元素时,root有数组children2[10],children2[0]中为null,children2[1]。。。您知道如何更改函数吗?我更改了if(child.children2[0]==null)leaves.add(root);现在我并没有得到错误,但在叶子中只有根的父,而不是叶,这是一个吗?另请参见:stackoverflow.com/help/someone-answers这不是最小的抱歉,但很高兴我解决了这个问题。此函数:`
public static void findLeaves(Tree root,List leaves){int k=0;if(root.i==0)leaves.add(root);while(k而且很好用,谢谢你的帮助。很棒的解决方案。我用这个模型在网页上获取多层阴影DOM中的底层元素。
treeArray[0]
data="d",                          children[] -    children[3]->data="c",children[0]->data="a", children[0]=null                                                                                                   
                                    /     \       
children[0]->data="b",children[0]=null    children[1]->data="b",children[0]=null