簡體   English   中英

為什么我的BST不能寫入文件?

[英]Why won't my BST get written to a file?

我正在嘗試將我的BST寫入文本文件,但有些文件不能正常工作。 我想知道我搞砸了哪里,因為截至目前,文件中沒有任何內容。 問題出在BinaryTree.java 我正在嘗試將項目放入Student.txt文件中的display()方法。

這是我的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*

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-with-resources非常有用。 字符集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);
}

我建議使用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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM