Tilted Forum Project Discussion Community

Tilted Forum Project Discussion Community (https://thetfp.com/tfp/)
-   Tilted Technology (https://thetfp.com/tfp/tilted-technology/)
-   -   [JAVA] Using a printwriter in an AVL tree (https://thetfp.com/tfp/tilted-technology/88261-java-using-printwriter-avl-tree.html)

89transam 05-01-2005 12:03 PM

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


a-j 05-01-2005 06:28 PM

Just create the writer before calling getTreeInfo and pass the writer to the method:

Code:

private void getTreeInfo( AvlNode t, Writer out) throws IOException
Then adjust your recursive calls to getTreeInfo(t.left, out), and symmetrically for the right side.

SiNai 05-01-2005 07:37 PM

What he said :thumbsup:

89transam 05-02-2005 09:12 PM

Yeah i ended up getting it. Just had to sleep on it, Thanks for the help though


All times are GMT -8. The time now is 07:56 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76