Why won't my BST be written to a file?

I am trying to write my BST to a text file, but some of them are not working. I would like to know where I messed up because nothing is being written to the file at the moment. The problem is BinaryTree.java

. The method display()

is where I am trying to put the items in the file Student.txt

.

Here's mine 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;
    }
}

      

Here my 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 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.");
    }
}

      

Here's mine 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);
    }   

      

Here's my Student.txt

file:

*displays nothing*

      

+3


source to share


2 answers


There is no growing array in Java, so studentArray will not work.

Use recursion:

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);
    }
}

      



Useful to use try-with-resources. UTF-8 encoding allows any character in student names.

Alternatively to an array, use 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);
}

      

+1


source


I would suggest using a StringBuilder to create a string to display the tree value. Writing to the file must be in a different class, so it will be loosely coupled and in the function, depending on the type of traversal type, it will be printed. If you put in an array, then again you have to go through, which will increase the complexity, and also I prefer to use the Iterative way. My logic is using forward traversal.



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);
}

      

0


source







All Articles