2016-05-15 144 views
0

我在實現樹形結構時需要幫助,我創建了添加子樹的功能,它將數據添加到樹中,但添加子樹時似乎存在問題。Java將txt文件讀入樹形數據結構

我想樹是什麼樣子:

    Date 
      / |  \ 
     /  |  \ 
     /  |   \ 
    /   |   \ 
    20160101  20160102  20160103 
    /    |  |  \ 
12:00   13:00 12:00  13:00 
/ \   / \  |  / \ 
Here There  Here There Here Here  There 

例如TXT文件:

 
Date,Time,Location 
20160101,12:00,Here 
20160101,12:00,There 
20160102,13:00,Here 
20160102,13:00,There 
20160103,12:00, Here 
20160103,13:00, Here 
20160103,13:00, There 

的日期的輸出顯得精緻,它顯示了兩個日期,因爲我不希望相同的日期顯示兩次,但時間和地點是錯誤的。

預期:

 
20160101 
12:00 
Here 
There 
20160102 
13:00 
Here 
There 
20160103 
12:00 
Here 
13:00 
Here 
There 

實際:

 
20160101 
12:00 
Here 
There 
13:00 
Here 
There 
20160102 
12:00 
Here 
There 
13:00 
Here 
There 
20160103 
12:00 
Here 
There 
13:00 
Here 
There 

我明白任何幫助或反饋,我的代碼。

public class Tree { 
    List<Tree> children = new ArrayList<Tree>(); 
    Tree parent = null; 
    String data = null; 

    public Tree(String data) { 
     this.data = data; 
    } 

    public Tree(String data, Tree parent){ 
     this.data = data; 
     this.parent = parent; 
    } 

    public void addChild(String data) { 
     Tree child = new Tree(data); 
     child.parent = this; 
     Boolean match = false; 
     for (int i = 0; i < this.children.size(); i++) { 
      if (this.children.get(i).data.equals(child.data)) { 
       match = true; 
       break; 
      } 
     } 
     if (!match) { 
      this.children.add(child); 
     } 
    } 

    public void addChild(Tree child) { 
     Boolean match = false; 
     for (int i = 0; i < this.children.size(); i++) { 
      if (this.children.get(i).data.equals(child.data)) { 
       match = true; 
       break; 
      } 
     } 
     if (!match) { 
      this.children.add(child); 
     } 
    } 

    public static void main(String[] args) throws IOException { 
     long startTime = System.nanoTime(); 
     Scanner scanFile = new Scanner(new File("example.txt")); 
     String line = ""; 
     line = scanFile.nextLine(); 
     Tree parentNode = new Tree(line.split(",")[0]); 
     Tree dateNode = new Tree(null, parentNode); 
     Tree timeNode = new Tree(null, dateNode); 
     Tree locationNode = new Tree(null, timeNode); 
     System.out.println(parentNode.data); 

     while(scanFile.hasNext()) { 
      line = scanFile.nextLine(); 

      timeNode.addChild(line.split(",")[2]); 
      dateNode.addChild(line.split(",")[1]); 
      parentNode.addChild(line.split(",")[0]); 
     } 
     scanFile.close(); 



     for(int i =0; i < parentNode.children.size(); i++) { 
      System.out.println(parentNode.children.get(i).data); 
      for(int j = 0; j < dateNode.children.size(); j++) { 
       System.out.println(dateNode.children.get(j).data); 
       for(int k = 0; k < timeNode.children.size(); k++) { 
        System.out.println(timeNode.children.get(k).data); 
      } 
    } 

    long endTime = System.nanoTime(); 
    System.out.println("Time taken: " + (endTime - startTime)/1E9 + "s"); 
    } 
} 
+0

什麼是應該是你的樹邏輯是什麼?對於'parentNode'的每個元素,您的打印只會反覆從'dateNode'和'timeNode'打印相同的數據。 – RealSkeptic

+0

它應該爲'parentNode'的每個元素打印'timeNode'的所有子數據和'dateNode'的所有子數據,問題是'dateNode.children'的大小,'timeNode.children'只是1 – phat

+0

,問題是樹結構沒有意義(你沒有解釋你應該建立什麼樹),並且你的打印是錯誤的。掃描完成後,您不會爲'timeNode'和'dateNode'分配任何內容,因此它們會保留您掃描的最後一個值。你應該[編輯]問題,解釋樹的邏輯應該是什麼。學習使用調試器是非常重要的,以便您可以一步一步地按照程序進行操作,並查看節點真正包含的內容。 – RealSkeptic

回答

-1

嘗試這些:

List<String> lines = new ArrayList<>(); 
Map<String,Map<String,List<String>>> tree = new TreeMap<String,Map<String,List<String>>>(); 

    for(int i=0;i<lines.size();i++){ 

     String[] parts = lines.get(i).split(","); 

     if(!tree.containsKey(parts[0])){ 
      tree.put(parts[0],new TreeMap<String,List<String>>()); 
     } 

     Map<String,List<String>> tree2 = tree.get(parts[0]); 

     if(!tree.containsKey(parts[1])){ 
      tree2.put(parts[1],new ArrayList<String>()); 
     } 

     List<String> tree3 = tree2.get(parts[1]); 

     if(!tree3.contains(parts[2])){ 
      tree3.add(parts[2]); 
     } 

    }