Existing implementations of trees in Java?

I am looking for any implementation of pure Tree data structure for Java (which are not graphical in java.awt), preferably generic.

With a shared tree, I would like to add items that shouldn't be sorted and do something like this:

TreeNode anotherNode = new TreeNode();
node.add(anotherNode);

      

... and then I would like to traverse the nodes (so that I can save and save the structure to a file when I load the tree again from the same file).

Does anyone know what implementations exist or is there any other idea to achieve this?

+2


source to share


4 answers


You can use the DefaultMutableTreeNode defined in the package javax.swing.tree

. It contains methods getUserObject()

and setUserObject(Object)

that allow you to attach data to each node tree. It allows an arbitrary number of child nodes for each parent, and provides methods for iterating through the tree using the width or depth ( breadthFirstEnumeration()

/ depthFirstEnumeration()

) method .



Also note that, although resident in the package javax.swing.tree

, this class does not contain any user interface code; This is just a basic model JTree

.

+3


source


Scala has a good data structure Tree

. This is the "Overall Balanced Tree". It's not really Java, but it is close and can serve as a good model.



Hard to believe given how many core Java libraries are in there, but there is no good overall structure Tree

.

+2


source


For starters, TreeSet and TreeMap are red-black tree implementations at runtime.

0


source


Assuming you don't want to store arbitrary Java objects on nodes, you can use the W3C DOM . It even comes with its own serialization format (I forgot what it called :-).

0


source







All Articles