Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am trying to write a code that creates new files and write the inorder, preord

ID: 3560132 • Letter: I

Question

I am trying to write a code that creates new files and write the inorder, preorder, postorder of a BST to those files. I keep getting errors at the 3 lines marked (void type not allowed here). How do I fix this?

public static void printToFiles(BinarySearchTree tree) throws FileNotFoundException {
PrintWriter preFile = new PrintWriter(new File("preorder.txt"));
PrintWriter inFile = new PrintWriter(new File("inorder.txt"));
PrintWriter postFile = new PrintWriter(new File("postorder.txt"));
  
// Traverse tree
System.out.print("Preorder: ");
tree.preorder();
System.out.print(" Inorder: ");
tree.inorder();
System.out.print(" Postorder: ");
tree.postorder();
  
preFile.print("Preorder: ");  
preFile.print(tree.preorder());       // ERROR HERE
inFile.print("Inorder: ");
inFile.print(tree.inorder());       // ERROR HERE
postFile.print("Postorder: ");
postFile.print(tree.postorder());       // ERROR HERE


System.out.println(" The preorder resulting list is stored " +
"in preorder.txt file.");
  
System.out.println("The inorder resulting list is stored " +
"in inorder.txt file.");
  
System.out.println("The postorder resulting list is stored " +
"in postorder.txt file.");
  
preFile.close();
inFile.close();
postFile.close();
  
  
}
  
}

Explanation / Answer

What is the return type of the functions, preorder(), inorder(), postorder() ? Make sure they return a value so that the argument for inFile, preFile and postFile does not return a void as void cannot be converted to string type.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote