2012-10-25 63 views
0

請有人幫助!!我是一個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,但我不知道如何設置孩子第一次。請幫助... :(

public class Tree { 
private Node root; 

public Tree(String rootData) 
{ 
    root=new Node(); 
    root.data=rootData; 
    root.children=new ArrayList<Node>(); 
} 
public Tree() { 
    super(); 
} 
public Node getRoot(){ 
    return this.root; 
    } 
public void setRoot(Node rootElement) { 
    this.root = rootElement; 
} 
} 

和Node類

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; 
} 
} 

和測試類

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; 
} 

}

回答

1

在你Node類的兩個構造,加此聲明在super之後致電: -

children = new ArrayList<Node>(); 

這將實例化您的List children

public Node() { 
    super(); 
    this.children = new ArrayList<Node>(); 
} 

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

此外,您還可以改變從1-arg and 0-argTree構造: -

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

public Tree() { 
    super(); 
} 

到下面一個,將使用該參數的構造函數實例化Node: -

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

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

P .S: -

如果您只想調用超類0-arg構造函數,則不需要明確添加super()調用。編譯器默認添加此調用。

+0

謝謝Rohit。有用。但是在currentNode中的孩子在多次迭代之後都是空的。任何想法爲什麼?? :( – user1688404

+0

@ user1688404。我認爲你需要改變你的'''''類構造函數也看到我更新的帖子 –

+0

Thanku再次。我認爲我的實現在某種程度上是不正確的。 – user1688404