05-01-2005, 12:03 PM | #1 (permalink) |
Psycho
Location: Central California
|
[JAVA] Using a printwriter in an AVL tree
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(); }
__________________
I'd rather be rich than stupid. Last edited by 89transam; 05-01-2005 at 12:07 PM.. |
05-01-2005, 06:28 PM | #2 (permalink) |
Tilted
|
Just create the writer before calling getTreeInfo and pass the writer to the method:
Code:
private void getTreeInfo( AvlNode t, Writer out) throws IOException |
Tags |
avl, java, printwriter, tree |
|
|