Java 在同一标签的n元树中搜索最低(最深级别)节点

Java 在同一标签的n元树中搜索最低(最深级别)节点,java,arraylist,tree,Java,Arraylist,Tree,这是我的n元树的节点类 import java.util.ArrayList; public class Node{ public String label; public ArrayList<Node> children; public Node(String label){ this.label =label; this.children = new ArrayList<Node>(); } p

这是我的n元树的节点类

import java.util.ArrayList;

public class Node{
    public String label;
    public ArrayList<Node> children;
    public Node(String label){
        this.label =label;
        this.children = new ArrayList<Node>();
    }
    public void addChild(String child){
        Node childNode = new Node(child);
        this.children.add(childNode);
    }
    public void addChild(Node child){
        this.children.add(child);
    }
    public Node findNode(String label){
        if(this.label.equals(label))
            return this;
        for(Node child : this.children)
            if (child.findNode(label) != null)
                return child.findNode(label);
        return null;
    }
}
import java.util.ArrayList;
公共类节点{
公共字符串标签;
公共ArrayList儿童;
公共节点(字符串标签){
this.label=标签;
this.children=new ArrayList();
}
公共void addChild(字符串子项){
节点子节点=新节点(子节点);
this.children.add(childNode);
}
公共void addChild(节点子节点){
this.children.add(child);
}
公共节点findNode(字符串标签){
if(this.label.equals(label))
归还这个;
for(节点子节点:this.children)
if(child.findNode(label)!=null)
返回child.findNode(标签);
返回null;
}
}
我需要一个类似“
Node findNode(String label)
”的方法,但可以使用相同的标签提供尽可能低的节点

此图中的示例


试试这样的东西,这只是个主意,我还没试过。 您需要计算已通过的奇数节点数

public static int max_depth = 0;
public static Node max_Node = null;
public Node findNode(String label){
   int counter = 0;
    if(this.label.equals(label))
        if(max_depth < counter){
            max_Node = this;
            max_depth = counter;
        }

    for(Node child : this.children){
         if (child.findNode(label) != null){
              if (child.findNode(label) != null){
                  counter++;
                   Node findNode(String label);
              }
         }

    }
  }
公共静态int max_depth=0;
公共静态节点max_Node=null;
公共节点findNode(字符串标签){
int计数器=0;
if(this.label.equals(label))
if(最大深度<计数器){
max_Node=this;
最大深度=计数器;
}
for(节点子节点:this.children){
if(child.findNode(label)!=null){
if(child.findNode(label)!=null){
计数器++;
节点findNode(字符串标签);
}
}
}
}
修改
Node.class
以存储另一个变量
int height
,该变量是在从
rootNode
遍历
树objectTree
时派生的

默认情况下,rootNode.getHeight()为0。从
Node.class
中的
rootNode
函数进行简单遍历,如下所示:

//note : This assumes you are at any Node. The root Node will have height = 0 ~important
public Node findLabel(String label){ 
   Node tempNode = null;
   assert this.getHeight()!=null;
   if(this.label.equals(label)){
     tempNode = this;
   }
   //traverse this' children 
   for(Node child : this.children){
         child.setHeight(this.getHeight()+1); //setting children height
         Node t = child.findNode(label);
         if(t!=null){
           if(tempNode!=null){ 
              if(tempNode.getHeight()>t.getHeight()){
                 tempNode=t;
              }
           }else{
             tempNode=t; //set TempNode = t as tempNode is null as this is our first solution
           }
         }      
    }
  return tempNode; //can be null and Contains the lowest node in the tree with label
} 

在修改了一些之后,我想出了这个似乎是正确的答案

代码如下:

import java.util.ArrayList;

public class Node{
    public String label;
    public ArrayList<Node> children;
    private int level;
    public Node(String label){
        this.level =0;
        this.label =label;
        this.children = new ArrayList<Node>();
    }

    public void addChild(String label){
        Node child = new Node(label);
        child.setLevel(this.getLevel()+1); //setting children level
        this.children.add(child);
    }
    public void addChild(Node child){
        child.setLevel(this.getLevel()+1); //setting children level
        this.children.add(child);
    }
    public Node findNode(String label){
        if(this.label.equals(label))
            return this;
        for(Node child : this.children)
            if (child.findNode(label) != null)
                return child.findNode(label);
        return null;
    }

    //note : This assumes you are at any Node searching for a child node. The root Node will have level = 0 ~important
    public Node findDeepNode(String label){
        Node tempNode = null;
        if(this.label.equals(label)&&this.findNode(label)==null){
            tempNode = this;
        }
        //traverse this' children
        for(Node child : this.children){
            Node t = child.findNode(label);
            if(t!=null){
                if(tempNode!=null){
                    if(tempNode.getLevel()<t.getLevel() && t.findDeepNode(label)==null){
                        tempNode=t;
                    }else tempNode=t.findDeepNode(label);
                }else{
                    if(t.findDeepNode(label)==null)
                        tempNode=t; //set TempNode = t as tempNode is null as this is our first solution
                    else tempNode=t.findDeepNode(label); //set TempNode = t.findDeepNode(label) as t has a child of the same label to put in tempNode instead
                }
            }
        }
        return tempNode; //can be null and Contains the lowest node in the tree with label
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }
    public static void main(String[] args){
        Node node=new Node("E");
        node.addChild("E-");
        node.addChild("T-");
        node.addChild("7");
        node.findNode("E-").addChild("F");
        node.findNode("E-").addChild("T");
        node.findNode("T-").addChild("8");
        node.findNode("T-").addChild("9");
        node.findNode("9").addChild("9");
        node.findDeepNode("9").addChild("T");
        System.out.println(node.findDeepNode("T").level);
    }
}
import java.util.ArrayList;
公共类节点{
公共字符串标签;
公共ArrayList儿童;
私有整数级;
公共节点(字符串标签){
这个水平=0;
this.label=标签;
this.children=new ArrayList();
}
公共void addChild(字符串标签){
节点子节点=新节点(标签);
child.setLevel(this.getLevel()+1);//设置子级
this.children.add(child);
}
公共void addChild(节点子节点){
child.setLevel(this.getLevel()+1);//设置子级
this.children.add(child);
}
公共节点findNode(字符串标签){
if(this.label.equals(label))
归还这个;
for(节点子节点:this.children)
if(child.findNode(label)!=null)
返回child.findNode(标签);
返回null;
}
//注意:这假设您在任何节点上搜索子节点。根节点的级别将为0
公共节点FindDepNode(字符串标签){
Node tempNode=null;
if(this.label.equals(label)&&this.findNode(label)==null){
tempNode=这个;
}
//遍历这个“孩子们”
for(节点子节点:this.children){
Node t=child.findNode(标签);
如果(t!=null){
if(tempNode!=null){

老实说,如果(tempNode.getLevel())非常令人困惑。那么“Node findDeepNode(String label);”指的是什么?返回语句在哪里?您不需要修改方法“findNode(String label)”,它的目的是创建一个类似“Node findDeepNode(String label);”的其他方法更好吗?在addchild方法中设置高度更好,因为在此代码中为“t”将有一个级别==0,因此比较是无用的,考虑到这一点,但是考虑到通常情况下人们无法预测
rootNode
的更改何时是不可预测的,并且更难将其可视化。根节点必须设置为0,其余节点基于此进行迭代。如果我错了,你能解释一下吗?我已经给出了一个答案,你可以看到如果有必要you@B15哈哈,对不起!我直到现在才看到。如果我要创建3个节点,然后(为了简单起见a
1-->2-->3
)我会先创建
3
并指定高度2。但是如果我想重新格式化,我会觉得我必须手动修改高度。我想我可以在迭代中解决它!但我只是想让它简单一些!希望它适用于在同一级别上为2'T返回哪个节点?当一个'T'是另一个'T'的子节点时,返回哪个节点“(或进一步向下的子树)?@rsp 1st可能是您首先添加的一个,对于我的用例,这不会是一个问题。2nd-(当一个“T”是另一个“T”的子节点时,子节点返回为最低的节点)正常情况下,但确实存在error@rsp第二,当一个“T”是另一个“T”的子节点时,子节点返回,因为它是最低的(可能在上次编辑后修复)