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!!

12-15-2008, 03:43 AM

Fubarable

an XML file perhaps?

12-15-2008, 03:48 AM

ExplosiveWeasel

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.

12-15-2008, 04:46 AM

Fubarable

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

12-15-2008, 04:47 AM

Fubarable

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.

12-15-2008, 04:48 AM

ExplosiveWeasel

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.

12-15-2008, 04:51 AM

Fubarable

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.

12-16-2008, 12:53 AM

Fubarable

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.

12-16-2008, 06:23 AM

ExplosiveWeasel

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?

12-16-2008, 06:26 AM

Fubarable

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?

12-16-2008, 06:58 AM

ExplosiveWeasel

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:

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

12-16-2008, 07:16 AM

ExplosiveWeasel

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.

12-16-2008, 07:16 AM

Fubarable

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.

12-16-2008, 07:20 AM

ExplosiveWeasel

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

12-17-2008, 01:13 AM

ExplosiveWeasel

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.