how to save this tree like class / data structure?

K

Kaidi

Hello all,
I have a class, which contains "pointers" so that it can construct a
tree like data structure (actually, I use it to store the tag tree I
built from a HTML file).

The problem is: how to save / load this class?

==== The class looks like: ==========

public class TagTreeNode implements Serializable {
//
private
String stringValue;
private
int nodeID;
private
int level;
private
String tagString;
private
TagTreeNode previousSibling;
private
TagTreeNode nextSibling;
private
TagTreeNode firstChild;
private
TagTreeNode parentNode;
//
// other functions etc.
}

========
Currently, I build a tree structure in which each node is of above
class.
I have the root node called rootNode.
And I try to save the whole tree using the following code:
==============
File thefile = new File(filename);
FileOutputStream fout = new FileOutputStream(thefile);
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(rootNode);
oos.close();
fout.close();
=======
This works fine for smaller trees. For biggers trees (actually, not so
big, only about 1000 - 2000 nodes in the tree, built from a HTML file
of 110K size), I always get the java.lang.StackOverflowError error.

PS: I have tried these: -Xss200M -Xmx200M -mx200M -Xms200M, seems no
use.

Any one can tell me either:
how to avoid the error?
or
how can I (in a good manner) save such a tree data structure onto file
? (and load it later)

Thanks a lot and have a great holiday!~
 
E

Elliott C. Bäck

how can I (in a good manner) save such a tree data structure onto file
? (and load it later)

Try writing a method to traverse the tree that passes along your
fileOutputStream, and writes the data out as it goes, rather than traversing
the tree in its entirety, and then writing out the data.
 
K

Kaidi

Hello all,
I have a class, which contains "pointers" so that it can construct a
tree like data structure (actually, I use it to store the tag tree I
built from a HTML file).

The problem is: how to save / load this class?

==== The class looks like: ==========

public class TagTreeNode implements Serializable {
//
private
String stringValue;
private
int nodeID;
private
int level;
private
String tagString;
private
TagTreeNode previousSibling;
private
TagTreeNode nextSibling;
private
TagTreeNode firstChild;
private
TagTreeNode parentNode;
//
// other functions etc.
}

========
Currently, I build a tree structure in which each node is of above
class.
I have the root node called rootNode.
And I try to save the whole tree using the following code:
==============
File thefile = new File(filename);
FileOutputStream fout = new FileOutputStream(thefile);
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(rootNode);
oos.close();
fout.close();
=======
This works fine for smaller trees. For biggers trees (actually, not so
big, only about 1000 - 2000 nodes in the tree, built from a HTML file
of 110K size), I always get the java.lang.StackOverflowError error.

PS: I have tried these: -Xss200M -Xmx200M -mx200M -Xms200M, seems no
use.

Any one can tell me either:
how to avoid the error?
or
how can I (in a good manner) save such a tree data structure onto file
? (and load it later)

Thanks a lot and have a great holiday!~

PS: Surely I can "walk" through the tree, write out each node. But I
just wonder any better way to save it?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top