Fog Creek Software
Discussion Board




Java Collection (or similar) for trees?

Right now I think I want/need an Java interface for a tree structure (NOT a Swing/AWT/etc tree view).

I can't remember or find anything that seems to suit, but have a nagging feeling such a thing would be quite useful. Given that DOM constructs an in memory tree I have a nagging feeling that other people might find such a thing useful as well... that and you know, the fact trees are a useful long established data structure and all.

Does anyone know of anything out there that provides a nice tree interface and some useful implementations (all I want is a bare basic implementation, but a variety of implementations would be nice)? After poking around the Jakarta Commons API, some Googling and thumbing through a couple of books the closest thing I can find is some code I could type in from a book and muck around with, but I'd rather download a zip or jar and save some keystrokes.

Walter Rumsby
Monday, November 10, 2003

Maybe you could use a treeview model?

the artist formerly known as prince
Monday, November 10, 2003

Treeview model would be okay assuming that it's not bound to a display API.

Walter Rumsby
Monday, November 10, 2003

How about the Java Generic Libraries from Objectspace:

http://www.recursionsw.com/products/jgl/jgl.asp

I haven't used these in quite some time (since the Java collections framework came on the scene), but they were really nice to use back in the day.

suggestion
Monday, November 10, 2003

A HashSet or a TreeSet may be what you're looking for:

http://java.sun.com/j2se/1.4.2/docs/guide/collections/overview.html

Nick
Monday, November 10, 2003

What about whatever class the use for dom trees?

the artist formerly known as prince
Tuesday, November 11, 2003

We use the Swing TreeNode classes and interfaces from the javax.swing.tree package in our server implementation in several places and it has greatly sped our development.  They are not tied to the Swing GUI controls other than that they are in the same package.

They implement several traversal orders that are nice.

Matt Lee
Tuesday, November 11, 2003

HashSet and TreeSet implement the Set interface, so I'd expect them to be missing some of the features I'm after (and Sets cannot contain duplicates).

JGL looks closest to what I'm after, but I'm starting to consider throwing something together in a SourceForge project.

Walter Rumsby
Tuesday, November 11, 2003

You might want to look at the various libraries made by scientists as well. Though you'll have to dig through a lot of sparse matrixes to get at one.

Li-fan Chen
Tuesday, November 11, 2003

I second the javax.swing.tree.TreeNode interface and javax.swing.tree.DefaultMutableTreeNode, which implements it.  They are not tied to GUI elements, and are good tree implementations.

If you're just looking to store things in a tree, but are not interested in the tree structure itself, try java.util.TreeMap.

Matt
Tuesday, November 11, 2003

*  Recent Topics

*  Fog Creek Home