2014-04-27 98 views
0

我試圖從文本文件創建決策樹。從文本文件寫入決策樹

public static BTNode<String> fileToTree (String fileName) throws IOException { 
    BufferedReader file = new BufferedReader(new FileReader(fileName)); 
    BTNode<String> node = new BTNode("", null, null); 
    BTNode<String> answer = fileToTreeHelper(node, file); 
    file.close(); 
    return answer; 
} 
    public static BTNode<String> fileToTreeHelper (BTNode<String> node, Scanner fileScanner) throws IOException { 
    String line; 

    if(node == null){ 
      node = new BTNode<String>(fileScanner.nextLine(), null, null); 
      fileToTreeHelper(node, fileScanner); 
    }else{ 
     if(fileScanner.hasNext()){ 
       if(node.isLeaf()){ 
        node.setData(fileScanner.nextLine()); 
       } 
       if(node.getLeft() == null) { 
        line = fileScanner.nextLine(); 
         if(line.contains("?")) { 
          node.setLeft(new BTNode<String>(line, null, null)); 
         } 
         if(line.contains(";")) { 
          node.setLeft(new BTNode<>(line,null, null)); 
          node.setRight(new BTNode<>(fileScanner.nextLine(),null, null)); 
         } 
        } 

       if(node.getRight() == null) { 
        line = fileScanner.nextLine(); 
        if(line.contains("?")) { 
         node.setRight(new BTNode<String>(line, null, null)); 
        } 
        if(line.contains(";")) { 
         node.getLeft().setLeft(new BTNode<>(line,null, null)); 
         node.getLeft().setRight(new BTNode<>(fileScanner.nextLine(),null, null)); 
         node.setRight(new BTNode<String>(line, null, null)); 
         fileToTreeHelper(node.getRight(), fileScanner); 
        } 
       } 
      } 
     } 
    return node; 
} 

這是我到目前爲止;當我運行它決策樹應該是這個樣子:

Are you a mammal? 
    Are you bigger than a cat? 
     Kangaroo; 
     Mouse; 
    Do you live underwater? 
     Trout; 
     Robin; 

但到目前爲止,所有我得到是這樣的:

Are you a mammal? 
    Are you bigger than a cat? 
     Kangaroo; 
     -- 
    -- 

如何做到這一點任何幫助嗎?我知道我需要遞歸調用這個函數,但是這不太好。有任何想法嗎?

+0

節點看起來像3個構造函數是什麼。另外我認爲每個問題都可能導致只有2個答案或問題? – LudgerP

+0

@路德耶是的每個問題只能有兩個答案。構造函數用於創建節點 – Daniel

回答

1

我覺得你的算法是混亂的,它應該是這樣的:

Node node = new Node(fileScanner.nextLine()) 
    // If the node is a question, it should have 2 subnodes with the answers/nested questions. 
    if(line.contains("?")){ 
     node.setLeft(fileToTreeHelper(...)); 
     // If there is an option that has only 1 answer, then this should have an if that 
     // checks if there is a ";" and then create the node or set it as null. 
     node.setRight(fileToTreeHelper(...)); 
    } 
    // If it is not a question, then it's just an answer to a previous question and returns. 
    return node; 
+0

如何遞歸調用fileToTreeHelper?我已經更新了它。它適用於在上下文中的小輸入 – Daniel

+0

,它將fileToTreeHelper(節點,fileScanner) – Troveldom

+0

如果它幫助你,請標記答案正確:) – Troveldom

0

我能解決這個問題。謝謝。現在完美運作。

public static BTNode<String> fileToTree (String fileName) throws IOException { 
     BufferedReader file = new BufferedReader(new FileReader(fileName)); 
     BTNode<String> node = null; 
     Scanner fileScanner = new Scanner (file); 
     BTNode<String> answer = fileToTreeHelper(fileScanner); 
     return answer; 
    } 

    public static BTNode<String> fileToTreeHelper (Scanner fileScanner) throws IOException { 
      BTNode node = null; 
      String line = fileScanner.nextLine(); 

      if(line.contains("?")){ 
       node = new BTNode<String>(line, null, null); 
       node.setLeft(fileToTreeHelper(fileScanner)); 
       node.setRight(fileToTreeHelper(fileScanner)); 

      }else{ 
       node = new BTNode<String>(line, null, null); 
       return node; 

      } 
      return node; 
    }