JAVA and Tree
Hi there
I got two phases of my question
First, I want to implement data structure TREE by JAVA. (Not Jtree in GUI). How do I do that?
Second, once I am done with a TREE, I need to save that into a database. What is the best way to save my TREE into a database?
take care
Mohammed Jubaer Arif
[329 byte] By [
Jubaera] at [2007-11-27 11:02:33]

A tree is such a broad categorization of data structures, that there's no single implementation in Java. There are classes in the API that use trees internally, but that's not really what you're asking, I think.
It's trivial to write some kind of class that can be used as a node to form some kind of tree. Here's one:
public class Node {
Object payload;
List<Node> children;
}
But you probably want something more specific and nuanced. Re-read your homework assignment to figure out exactly what you need to accomplish.
Thanks for Node class, I got an idea how to generate a tree and I will search for the API as well.
Now say I got a tree like this
a
/\
bc
Now the question is, I want to save this tree into a database. What is the best way to do it. Because my query to database will be give me the tree "a" and it should return me the whole tree as it is shown above.
The difficult part to me is, I can save easily some string into a database. Now if my query return me that string, how do I match it in JAVA with proper hierarchy?
I believe you could have a "child-of" table, that maps nodes to other nodes. Say, the first column has an identifier for the parent, and the second column has an identifier for a child. You'd have one row for every child of a given parent. You can also have a column to identify the particular parent/child relationship (though I'm not sure why you'd need this) and another useful column might be an ordering, if the children of a given parent are ordered.
This would be in addition to another table that simply lists each node in the tree.
I think that some object-relational mapping tools, such as Hibernate, can easily handle moving java classes into and out of such a set of tables.