【问题标题】:Why won't my BST get written to a file?为什么我的 BST 不会被写入文件?
【发布时间】:2017-05-13 21:10:43
【问题描述】:

我正在尝试将我的 BST 写入一个文本文件,但它无法正常工作。我想知道我在哪里搞砸了,因为到目前为止,没有任何东西被写入文件。问题出在BinaryTree.java。 display() 方法是我试图将项目放入 Student.txt 文件的地方。

这是我的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 algorithm file data-structures binary-tree


    【解决方案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-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);
    }
    

    【讨论】:

    • 感谢您的回答。非常感谢:D!请您进一步扩展ArrayList 部分好吗?具体来说,sa.get(i) 部分和 for (s : sa) { 部分。谢谢你:)。
    • 扩展答案
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-01
      • 1970-01-01
      • 2021-05-30
      相关资源
      最近更新 更多