Java 发展树形结构

Java 发展树形结构,java,Java,请有人帮忙!!我是Java的新手。我想从数组列表中创建一个树结构。我的意见是 1.1 1.2 1.3 1.3.1.1 1.3.1.2 1.4 1.4.1.1 1.4.2.1 我的目标是得到一棵像 1.1 1.2 1.3 1.4 1.3.1.1 1.4.1.1 1.5 1.3.1.2 1.4.1.2 等等 请在下面找到我的课程。我在tes

请有人帮忙!!我是Java的新手。我想从数组列表中创建一个树结构。我的意见是

1.1
1.2
1.3
1.3.1.1
1.3.1.2
1.4
1.4.1.1
1.4.2.1
我的目标是得到一棵像

               1.1
               1.2
               1.3
       1.4           1.3.1.1
 1.4.1.1   1.5          1.3.1.2    
 1.4.1.2
等等

请在下面找到我的课程。我在test.tree.Node.addChild(Node.java:28)中得到了一个nullPoniter,我知道这是因为'children'为null,但我不知道如何第一次设置子项。请帮忙(

公共类树{
私有节点根;
公共树(字符串根数据)
{
根=新节点();
root.data=rootData;
root.children=newarraylist();
}
公树(){
超级();
}
公共节点getRoot(){
返回这个.root;
}
公共void setRoot(节点根元素){
this.root=rootElement;
}
}
和节点类

class Node { 
String data;
Node parent;
List<Node> children;

public Node() {
    super();
}
public Node(String name)
{
    super();
    this.data=name;
}
public void addChild(String name) {
    this.addChild(new Node(name));
}
public void addChild(Node child) {
    this.children.add(child);
}
public void removeChild(Node child) {
    this.children.remove(child);
}
public void removeChild(String name) {
    this.removeChild(this.getChild(name));
}
public Node getChild(int childIndex) {
    return this.children.get(childIndex);
}
public Node getChild(String childName) {
    for (Node child : this.children) {
        if (child.data.equals(childName)) { return child; }
    }
    return null;
}
public List<Node> getChildren() {
    if (this.children == null) {
        return new ArrayList<Node>();
    }
    return this.children;
}
 public void setChildren(List<Node> children) {
    this.children = children;
}
public Node getParentNode() {
    return this.parent;
}
}
类节点{
字符串数据;
节点父节点;
列出儿童名单;
公共节点(){
超级();
}
公共节点(字符串名称)
{
超级();
this.data=名称;
}
public void addChild(字符串名称){
this.addChild(新节点(名称));
}
公共void addChild(节点子节点){
this.children.add(child);
}
public void removeChild(节点子节点){
这个。儿童。移除(儿童);
}
public void removeChild(字符串名称){
this.removeChild(this.getChild(name));
}
公共节点getChild(int childIndex){
返回此.children.get(childIndex);
}
公共节点getChild(字符串childName){
for(节点子节点:this.children){
if(child.data.equals(childName)){return child;}
}
返回null;
}
公共列表getChildren(){
if(this.children==null){
返回新的ArrayList();
}
把这个还给孩子们;
}
公共子项(列出子项){
这个。孩子=孩子;
}
公共节点getParentNode(){
将此文件返回给父对象;
}
}
测试类是

public class TreeTest {
public static void main(String[] args) {
    TreeTest tt = new TreeTest();
    ArrayList<String> newArr= new ArrayList<String>();
    newArr.add("1.1");
    newArr.add("1.2");
    newArr.add("1.3");
    newArr.add("1.3.1.1");
    newArr.add("1.3.1.2");
    newArr.add("1.4");
    newArr.add("1.4.1.1");
    newArr.add("1.4.2.1");
    int lCount=0;
    int maxCount= newArr.size();
    Tree tr= new Tree();
    Node rootNode = new Node();
    String parent_name=null;
    Node currentNode= new Node();
    for(String line: newArr){
        if(lCount==0){
        rootNode = tt.getTree(line);
        tr.setRoot(rootNode);
        currentNode= rootNode;
        }
        else{
            List<Integer> cur =  new ArrayList<Integer>();
            List<Integer> pre =  new ArrayList<Integer>();
            cur= tokenize(line);
            pre= tokenize(newArr.get(lCount-1));
            if(cur.size()==pre.size()){

                currentNode.addChild(tt.getTree(line));
                currentNode= tt.getTree(line);
                }
            else if (cur.size()>pre.size()){
                currentNode.addChild(tt.getTree(line));
                parent_name= newArr.get(lCount-1);
                currentNode= tt.getTree(line);
                }
            else if(cur.size()< pre.size()){
                currentNode= tt.getTree(parent_name);
                currentNode.addChild(tt.getTree(line));
                currentNode= tt.getTree(line);
            }
        }

        lCount++;
        }
        }
private Node getTree(String string) {
    // TODO Auto-generated method stub
        Node rootNode = new Node(string);
        return rootNode;
    }
private static List<Integer> tokenize(String line) {
    // TODO Auto-generated method stub
    List<Integer> line_Arr =  new ArrayList<Integer>();

    String[] tokens = line.split("\\.");
    int i=0;
    for(String atr: tokens)
        line_Arr.add(Integer.parseInt(atr));

    return line_Arr;
}
公共类树测试{
公共静态void main(字符串[]args){
TreeTest tt=新的TreeTest();
ArrayList newArr=新的ArrayList();
新协议新增(“1.1”);
新协议新增(“1.2”);
新协议新增(“1.3”);
新增(“1.3.1.1”);
新增(“1.3.1.2”);
新协议新增(“1.4”);
新增(“1.4.1.1”);
新增(“1.4.2.1”);
int lCount=0;
int maxCount=newArr.size();
Tree tr=新树();
Node rootNode=新节点();
字符串parent_name=null;
节点currentNode=新节点();
for(字符串行:newArr){
如果(lCount==0){
rootNode=tt.getTree(line);
tr.setRoot(rootNode);
currentNode=rootNode;
}
否则{
List cur=new ArrayList();
List pre=新建ArrayList();
cur=标记化(行);
pre=tokenize(newArr.get(lCount-1));
if(cur.size()==pre.size()){
currentNode.addChild(tt.getTree(line));
currentNode=tt.getTree(行);
}
else if(cur.size()>pre.size()){
currentNode.addChild(tt.getTree(line));
父项名称=newArr.get(lCount-1);
currentNode=tt.getTree(行);
}
else if(cur.size()

}

节点的两个构造函数中,在
super
调用后添加以下语句:-

children = new ArrayList<Node>();

此外,您还可以从以下位置更改
1-arg和0-arg
构造函数:-

public Tree(String rootData)
{
    root=new Node();
    root.data=rootData;
    root.children=new ArrayList<Node>();
}

public Tree() {
    super();
}
p.S:-


如果您只想调用超级类
0-arg
构造函数,则无需显式添加
super()
调用。编译器默认情况下会添加此调用。

谢谢Rohit。它可以工作。但经过多次迭代后,currentNode中的子级为null。知道原因吗??:(@user1688404.我想你也需要更改
类构造函数。再次查看我更新的帖子。谢谢。但我觉得我的实现有点不正确。@user1688404.是的,我看到了,你的代码看起来有点凌乱。你可以再看一次。或者,试着弄清楚流程到底应该是什么。
public Node() {
    super();
    this.children = new ArrayList<Node>();
}

public Node(String name)
{
    super();
    this.children = new ArrayList<Node>();
    this.data=name;
}
public Tree(String rootData)
{
    root=new Node();
    root.data=rootData;
    root.children=new ArrayList<Node>();
}

public Tree() {
    super();
}
public Tree(String rootData) {
    root=new Node(rootData);
}

public Tree() {
    root = new Node();
}