Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Tree Object in Java

 
Trevor Price
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Trevor Price
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, I'll have to look into this more then. I somewhat casually dismissed it. Thanks.
 
Dave Wingate
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also use TreeSet from the java.util package.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
amjad sharief
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic