Tilted Forum Project Discussion Community  

Go Back   Tilted Forum Project Discussion Community > Interests > Tilted Technology


 
 
LinkBack Thread Tools
Old 05-01-2005, 12:03 PM   #1 (permalink)
Psycho
 
89transam's Avatar
 
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..
89transam is offline  
Old 05-01-2005, 06:28 PM   #2 (permalink)
a-j
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
Then adjust your recursive calls to getTreeInfo(t.left, out), and symmetrically for the right side.
a-j is offline  
Old 05-01-2005, 07:37 PM   #3 (permalink)
Dreams In Digital
 
SiNai's Avatar
 
Location: Iowa
What he said
__________________
I can't seem to remember now
What it was like- to live life, before you.. symbiont
SiNai is offline  
Old 05-02-2005, 09:12 PM   #4 (permalink)
Psycho
 
89transam's Avatar
 
Location: Central California
Yeah i ended up getting it. Just had to sleep on it, Thanks for the help though
__________________
I'd rather be rich than stupid.
89transam is offline  
 

Tags
avl, java, printwriter, tree


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 10:08 PM.

Tilted Forum Project

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, 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