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.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.