2017-05-13 9 views
3

私は自分のBSTをテキストファイルに書き込もうとしていますが、動作していないものもあります。今のところ、ファイルに何も書き込まれていないので、私はどこでうんざりしたのか知りたいです。問題はBinaryTree.javaです。 display()メソッドは、項目をStudent.txtファイルに配置しようとしています。私のBSTがファイルに書き込まれないのはなぜですか?

ここに私のNode.javaです:

class Node { 
    Student data; 
    Faculty data2; 
    Node left; 
    Node right; 

    public Node(Student data) { 
     this.data = data; 
     this.left = left; 
     this.right = left; 
    } 

    public Node(Faculty data2) { 
     this.data2 = data2; 
     this.left = left; 
     this.right = right; 
    } 
} 

は、ここに私のBinaryTree.javaです:

int index = 0; 
String[] sa = new String[index]; 

public void studentArray() { 
    studentArray(root,index); 
} 

public int studentArray(Node root, int index) {  
    if(root.left != null) { 
     index = studentArray(root.left, index); 
    } 
    sa[++index] = root.data.getLastName().toString(); 
    if(root.right != null) { 
     index = studentArray(root.right,index); 
    } 
    return index; 
} 

public void displayStudent(Node root) throws IOException { 
    if(root != null) { // If root isn't empty. 
     if(root.left != null) { 
      displayStudent(root.left); // Recursively display left nodes. 
     } 
     System.out.println(root.data.toString()); // Print to the console data that's in the root in order. 
     if(root.right != null) { 
      displayStudent(root.right); // Recursively display right nodes. 
     } 
    } 

    String file = "Student.txt"; 
    FileWriter fw = new FileWriter(new File(file)); 

    try { 
     for(index = 0; index < sa.length; index++) { 
      fw.write(sa[index] + " "); 
     } 
     fw.close(); 
    } catch(Exception e) { 
     System.out.println("File not found."); 
    } 
} 

ここに私のMain.javaです:

import java.io.IOException; 

public class Main { 
    public static void main(String[] args) throws IOException { 
     Student student1 = new Student("Mike", "Piazza", "S3123456"); 
     Student student2 = new Student("Jack", "Jill", "S3123456"); 
     Student student3 = new Student("Alice", "Jones", "S3123456"); 

     BinaryTree bt = new BinaryTree(); 
     bt.insertStudent(student1); 
     bt.insertStudent(student2); 
     bt.insertStudent(student3); 
     bt.displayStudent(bt.root); 
    } 

ここに私のStudent.txtファイルです:

は、
*displays nothing* 

答えて

1

Javaはの配列を持たないため、studentArrayは機能しません。

いずれかを使用再帰:

try (PrintWriter out = new PrintWriter(file, "UTF-8")) { 
    print(out, root); 
} // automatically closes out 

void print(PrintWriter out, Node node) { 
    if (node != null) { 
     print(out, node.left); 
     out.println(...); 
     print(out, node.right); 
    } 
} 

のtry-と、リソースが便利です。 文字セットUTF-8は生徒の名前にサインインを許可します。

配列の代わりに、ArrayListを使用します。

List<String> sa = new ArrayList<>(); 

sa.add(root.data.getLastName().toString(); 

for (int i = 0; i < sa.size(); ++i) { // Old-style, if you need the index i 
    String s = sa.get(i); 
    ... 
    sa.set(i, s + s); 
} 

for (String s : sa) { 
    System.out.println(s); 
} 
+0

ご回答ありがとうございます。大変感謝しています:D! 'ArrayList'の部分をもう少し拡張してください。具体的には、 'sa.get(i)'部分と 'for(s:sa)'部分です。ありがとうございました :)。 – bojack

+0

答えを拡張しました –

0

私は木の値を表示するための文字列を作成するためのStringBuilderを使用することをお勧めします。 ファイルへの書き込みは、異なるクラスになければならないので、疎結合になり、印刷するトラバーサルタイプのタイプに応じて機能が変更されます。配列を挿入すると、複雑さが増し、さらに複雑になります反復的な方法を使用します。私のロジックは、事前注文トラバーサルを使用しています。

public String displayStudent(TreeNode root) { 
if (root == null) 
    return null; 
Stack <TreeNode> stack = new Stack <TreeNode>(); 
stack.push(root); 
StringBuilder sb = new StringBuilder(); 

while (!stack.isEmpty()) { 
    TreeNode h = stack.pop(); 
    if (h != null) { 
    sb.append(h.val + ","); 
    if (h.right != null) { 
    stack.push(h.right); 
    } 
    if (h.left != null) { 
    stack.push(h.left); 
    } 

    stack.push(h.left); 
    } 
} 

return sb.toString().substring(0, sb.length() - 1); 
} 
関連する問題