Results 1 to 17 of 17
  1. #1
    Join Date
    Dec 2008
    Posts
    9
    Rep Power
    0

    Default Help loading a Binary Tree from file

    Hi all,
    So I am working on making a 20 question game using a binary tree. I have set it up so each time the user plays, if the computer cannot guess what they are thinking of, they tell the computer and it saves it. I am saving the tree in a text file recursively, in a format that looks something like this:

    {
    Do you use it outside?
    {
    a shovel
    [null]
    [null]
    }
    {
    is it round?
    {
    a ball
    [null]
    [null]
    }
    {
    a TV
    [null]
    [null]
    }
    }
    }

    The problem is now, I am having a very hard time rebuilding the tree when the user restarts the game. As you can see, the "{" indicates the beginning of a path and the "}" is the end. Can someone suggest a nice way to load this back up into my data structure? Thanks!!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    an XML file perhaps?

  3. #3
    Join Date
    Dec 2008
    Posts
    9
    Rep Power
    0

    Default

    i had contemplated that, but when I attempted it, it seemed a bit over my head this point. I was really hoping that it may be possible to do it recursively when reading it back in, but so far I havent had any luck.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Just curious, do you know much about ArrayLists and other collections classes? HashMaps?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Cross-post: Java Programming - Help loading a binary tree from file
    Please know that cross-posting can frustrate anyone who tries to help you only to find out later that the same answer was given hours ago in a cross-posted thread. No one likes wasting their time, especially a volunteer. The polite thing to do would be to not do this, but if you feel that you absolutely must, to at least provide links in both cross-posts to each other.

  6. #6
    Join Date
    Dec 2008
    Posts
    9
    Rep Power
    0

    Default

    I know a good amount about all of those. I Actually, just finished a class on data structures so its still fairly fresh in my head.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Cool, then you should probably be able to figure out XML. You will need to store your data in some hierarchical structure (I think), and XML is usually pretty good at this. Note that I'm not an expert in this however. Good luck.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You know the more that I thought about this, the more I came to the conclusion that serializing your binary tree seems to be a viable answer. Why create an artificial text file? Why mess with XML when serialization is simple and effective? I'd love it if one of the true experts here weighed in on this.
    Last edited by Fubarable; 12-16-2008 at 05:30 AM.

  9. #9
    Join Date
    Dec 2008
    Posts
    9
    Rep Power
    0

    Default

    that is actually a brilliant idea. I am wrestling with it now, but am not sure how exactly the Serializable interface is implemented with my tree. It keeps throwing this "java.io.notserializable" exception. Any thoughts?

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I'm in over my head here, but I wonder:
    1) What binary tree are you using specifically?
    2) the objects held by the tree, do they implement the Serializable interface?
    Last edited by Fubarable; 12-16-2008 at 05:31 AM.

  11. #11
    Join Date
    Dec 2008
    Posts
    9
    Rep Power
    0

    Default

    Ok so,
    I am using a tree that I built myself, its fairly simple and the objects inside are nodes. They do indeed implement Serializable and tracing through the call stack is all but useless for this. This is what my main looks like so far:

    twentyQuestEC alpha = new twentyQuestEC();

    /* FileInputStream fis = new FileInputStream( "tree.txt" );
    ObjectInputStream inStream = new ObjectInputStream( fis );

    alpha = ( twentyQuestEC )inStream.readObject();
    */

    FileOutputStream fos = new FileOutputStream( "tree.txt" );
    ObjectOutputStream outStream =
    new ObjectOutputStream( fos );

    alpha.question();
    outStream.writeObject( alpha ); <--- line that throws error
    outStream.flush();

  12. #12
    Join Date
    Dec 2008
    Posts
    9
    Rep Power
    0

    Default

    it is bizzare because when I look at the "tree.txt" file afterwords, there is a whole bunch of nonsense in there, so something is happening. Weird
    Last edited by ExplosiveWeasel; 12-16-2008 at 06:06 AM.

  13. #13
    Join Date
    Dec 2008
    Posts
    9
    Rep Power
    0

    Default

    Lol, sorry for all the replies, but I figured out why it wasn't saving, the scanner is not serializable. So when I declare it, it has to be transient. So now the problem is in the alpha = ( twentyQuestEC )inStream.readObject(); line. It says that there is an EOFexception. Im not sure why this is happening, but it usually happens when the file ends before expected, so something must not be saving properly. Not really sure.

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    If you are now trying to serialize the data and store it in a file, then that file is no longer a "text" file but instead is a binary file. If you try to view it with a text viewer, you will see only gibberish.

  15. #15
    Join Date
    Dec 2008
    Posts
    9
    Rep Power
    0

    Default

    right, I see that now. Do you have any idea why it might be throwing a EOFException while reading it back in now?

  16. #16
    Join Date
    Dec 2008
    Posts
    9
    Rep Power
    0

    Default

    good news, I was tweaking with Serialization and things are working flawlessly. So simple, yet so effective. Thanks so much for all your help again.

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. Level order binary tree traversal
    By H3rtaherta in forum New To Java
    Replies: 1
    Last Post: 04-20-2009, 07:34 AM
  2. Binary Search Tree
    By michael_mke in forum New To Java
    Replies: 3
    Last Post: 12-04-2008, 02:03 AM
  3. Binary Search Tree Traversal
    By dch414 in forum New To Java
    Replies: 2
    Last Post: 11-07-2008, 12:01 AM
  4. Can anybody help with cuncurrent binary search tree guys)
    By danylo in forum Threads and Synchronization
    Replies: 1
    Last Post: 04-23-2008, 06:22 PM
  5. Binary Tree Implementation in Java
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •