Is anyone aware of a generic tree (nodes may have multiple children) implementation for Java? It should come from a well trusted source and must be fully tested.
It just doesn't seem right implementing it myself. Almost reminds me of my university years when we were supposed to write all our collections ourselves.
EDIT: Found this project on java.net, might be worth looking into.
Use Guava
Guava 15.0 introduces a nice API for tree traversal so you don't need to re-implement it for the gazillionth time in your codebase.
Namely,
TreeTraverser
and some specialized implementations, likeBinaryTreeTraverser
.A very much welcome addition to avoid re-implementing something so simple and with added bonus:
While You're There...
Notice that Guava also provides now new methods to its
Files
utility class that make use of theTreeTraverser
, e.g.Files.fileTreeTraverser()
which gives you aTreeTraverser<File>
for your file-system traversal needs.I found an implementation of a Generic Tree (with tests) here:
http://vivin.net/2010/01/30/generic-n-ary-tree-in-java/
I think this is what you are looking for.
I found an absolutely fantastic library http://jung.sourceforge.net, see the javadoc http://jung.sourceforge.net/doc/api/index.html . It is much more than just a graph implementation. With it you can visualize and layout graphs; plus, it has a bunch of standard graph algorithms you can use out of the box. Go, check it out! Although I ended up implementing my own basic graph (I didn't know of JUNG before), I use this library for visualization. It looks very neat!
It's rather hard to do a true generic tree implementation in Java that really separated the tree operations and properties from the underlying implementations, i.e. swap in a RedBlackTreeNode and override a couple of method to get a RedBlackTree implementation while retaining all the generic operations that a BinaryTree interface contains.
Also, an ideal abstraction would be able to swap out the low-level tree representation, e.g. an implicit binary tree structure stored in an array for a Heap or a Node-base interface with left and right child pointers, or multiple child pointers, or augmenting any of the above with parent pointers, or threading the leaf nodes, etc, etc, etc.
I did try and solve this myself, but ended up with quite a complicated interface that still enforces type safety. Here's the skeleton of the idea that sets up a abstract BinaryTree class with a non-trivial operation (Euler Tour) that will work even if the underlying node class or tree class is changed. It could probable be improved by introducing the idea of cursors for navigation and positions within the tree structure:
Here it comes:
I am well trusted, but haven't tested the implementation.
Ah, I was going to post a shameless plug to my solution and saw that someone already posted a link to it. Yeah, I had the same issue and I basically ended up writing my own Generic Tree. I've got tests for the tree node and the tree itself.
I implemented the node as an object having a data field and a list of nodes (which are the children of that node).
http://vivin.net/2010/01/30/generic-n-ary-tree-in-java/