aspose file tools*
The moose likes Beginning Java and the fly likes Tree Object in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Tree Object in Java" Watch "Tree Object in Java" New topic
Author

Tree Object in Java

Trevor Price
Greenhorn

Joined: May 19, 2006
Posts: 16
Hi. I'm trying to find some sort of tree-structured object for Java, similar to the JTree. However, I'm using it just to keep track of data, not for a GUI. This is, actually, a wep application.

Is there a suitable class that isn't GUI oriented?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,

Welcome to JavaRanch!

Not many people appreciate that TreeModel is actually deliberately GUI-agnostic. If you look at, for example, DefaultMutableTreeModel, you'll see that despite being in the Swing class hierarchy, it makes no reference at all to Swing components. I'm not saying it's the most elegant tree data structure in the world, but the price is right and it serves.


[Jess in Action][AskingGoodQuestions]
Trevor Price
Greenhorn

Joined: May 19, 2006
Posts: 16
Hmm, I'll have to look into this more then. I somewhat casually dismissed it. Thanks.
Dave Wingate
Ranch Hand

Joined: Mar 26, 2002
Posts: 262
You could also use TreeSet from the java.util package.


Fun programming etcetera!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Dave Wingate:
You could also use TreeSet from the java.util package.


Mmmmm, not so much. TreeSet and its underlying TreeMap are based internally on a tree implementation, but they don't expose the tree so you can use it for anything.
Dave Wingate
Ranch Hand

Joined: Mar 26, 2002
Posts: 262
Ah, guess I didn't understand what Trevor was wanting to do with the Tree data structure. I agree that TreeSEt wouldn't work if he needs the ability to access/manipulate arbitrary nodes in the set.
amjad sharief
Greenhorn

Joined: Aug 10, 2006
Posts: 2
see i would like to encapsulate some business object as a tree. For example i would like to create a tree structure like this.

Product
|
|
|----Product
| |
| |-----Product
|
|
|----Product

If you see here, The "Product" is the root node as well as child node. we have some requirement of modeling like this kind of tree structure.

Is there any framework or API which will be useful for us to start with?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
TreeModel is probably it. If you find it entertaining to make your own, a Node with references to LeftNode and RightNode or maybe a collection of ChildNodes is pretty easy to get going.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
amjad sharief
Greenhorn

Joined: Aug 10, 2006
Posts: 2
Thanks Mr.Stans. Probaly the time has come for the opensource community to think about this requirement and make a framework for modeling a tree hieararchy. I am open for that. I will try to create a project in open source for this purpose and will post the details here in this forum. It will be very helpful if most java developers get involved in the thought process and make that framework a success.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Tree Object in Java