Yes this is homework, but this is not supposed to be the part that trips us up.
The goal is to print an AVL tree in order to a file. I chose to go with a printwriter because it deals with strings easilly. I can get the correct output to a regular System.out command , but I cannot translate that to a file.
I know that the problem is that every time I call the getTreeInfo method recursivly I am creating a new instance of my file and print writers and it is just overwriting the same data, but I cant think of a way around this. Any help would be awesome. I dont want code , just any nudge in the right direction. ( I know this should be simple)
Code:
private void getTreeInfo( AvlNode t ) throws IOException
{
FileWriter writer = new FileWriter("output.txt");
PrintWriter out = new PrintWriter(writer);
if( t != null )
{
getTreeInfo(t.left);
out.println(nodeInfo(t));
out.println("\t" + "Left: " + "\t" + nodeInfo(t.left));
out.println("\t" + "Right: " + "\t" + nodeInfo(t.right));
getTreeInfo(t.right);
}
out.close();
}