0

Possible Duplicate:
Java tree data-structure?

Is there any Java data structure implementation similar to tree and graph?

Community
  • 1
  • 1
user236501
  • 8,538
  • 24
  • 85
  • 119

2 Answers2

4

Not in the java.util Collections API.

You can use the DefaultTreeModel from Swing for trees.

Jung is a graph framework.

duffymo
  • 305,152
  • 44
  • 369
  • 561
0

As I answered for a similar question, the Java API contains no general API for trees/graphs, since there is no unique set of features needed in every usecase. There are quite some tree/graph-like APIs for special cases, though.

And in principle it is easy to make your own graph - one could even say that every object is in fact a node in a graph, with the values of its reference type fields being the (outgoing) neighbors.

Community
  • 1
  • 1
Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
  • Not true for tree: DefaultTreeModel in Swing is stand-alone enough where you can use it without a UI. – duffymo May 04 '11 at 13:10
  • I listed this in my linked answer, too. I don't like DefaultTreeModel, I'm even with Swing usually using my own TreeModel implementation, so I would never have the idea to use it outside of Swing. – Paŭlo Ebermann May 04 '11 at 17:23
  • What don't you like about it? How does yours improve on it? – duffymo May 05 '11 at 23:38
  • DefaultTreeModel is usually too complicated, for my view, depending on what you want to do. I usually make a new model for each use case, based on the domain model (i.e. the self made object tree as mentioned in my answer) instead of some TreeNode implementation (which is the basic tree interface behind DefaultTreeModel). Of course, the javax.swing.tree package is missing an AbstractTreeModel implementing the listener stuff, which seems to lead to use DefaultTreeModel, even though it is more than needed. – Paŭlo Ebermann May 06 '11 at 08:52