• Post Reply Bookmark Topic Watch Topic
  • New Topic

Tree-like data structure question  RSS feed

 
justin mcsporran
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
First up, apols if this oughtta be in a more advanced forum <genuflects to the moderator/>.
I'm wondering if anyone could help me please with a simple tree-like data structure/object in Java?
It needs to have methods to allow adding, deleting and getting parents and children. Those parents and children could be references to hash-like objects (ideally), or be data objects on which one could set and get attributes.
I'll be using it to represent data returned from a database using a 3rd party API (a content management system, for the curious), so there's no direct access to the database itself. However, the schema design is tree-like, hence my request.
I know this might sound like something for XML, but, I only need to build these structures in memory for brief periods of time, without the management overhead of storing state info in actual documents.
Any advice would be warmly appreciated.
Tks et rgds,
Justin
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could build a very generic "node" that could maintain a hierarchy of associated objects. Something like:

The thing that receives data from your 3rd party API and builds the tree will probably keep a stack or array to represent the current path down the tree to the node we're working on now. Not knowing how your data will look, I can't guess too much about how the builder will work.
Say the API gives you an object and its nesting depth in the tree. You might do something like this:

If your data gives you some other set of parameters, you'll have to figure out an algorithm for inserting children.
Is this the level of help you were looking for?
[ June 16, 2003: Message edited by: Stan James ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!