Tilted Forum Project - TFP - Sexuality, Philosophy and Political Discussion

Go Back   Tilted Forum Project - TFP - Sexuality, Philosophy and Political Discussion > Interests > Tilted Technology

Reply
 
LinkBack Thread Tools
Old 05-01-2005, 02:03 PM   #1 (permalink)
Psycho
 
89transam's Avatar
 
Join Date: Apr 2003
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 02:07 PM.
89transam is offline   Reply With Quote
Old 05-01-2005, 08:28 PM   #2 (permalink)
a-j
Tilted
 
Join Date: Sep 2004
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   Reply With Quote
Old 05-01-2005, 09:37 PM   #3 (permalink)
Dreams In Digital
 
SiNai's Avatar
 
Join Date: Oct 2003
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   Reply With Quote
Old 05-02-2005, 11:12 PM   #4 (permalink)
Psycho
 
89transam's Avatar
 
Join Date: Apr 2003
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   Reply With Quote
Reply

Bookmarks

Tags
avl, java, printwriter, tree

Thread Tools

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
Forum Jump


All times are GMT -7. The time now is 01:49 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0
All text (c) 2002-2008 Tilted Forum Project
"Insignia" vBulletin 3.5 - b6gm6n - x7x7x7.com