For Programmers: Free Programming Magazines  


Home > Archive > Java Help > September 2006 > Tree data structure in Java









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Tree data structure in Java
Allan M. Bruce

2006-09-18, 10:03 pm

I am hoping to use a tree data structure in my Java application. I need to
be able to create a tree which I can add next nodes to certain paths and
obtain a list of all possible paths in the tree. Is there an implementation
of this built into Java? For example, I would like to create a structure
similar to this:

A1
/ \
M1 C3
/ | \ / \
D S E R Ts
/\
4 6

Basically each node is labelled with a String, I dont want the data being
sorted. And I want to iterate through all paths to get a list like:

A1, M1, D
A1, M1, S
A1, M1, E
A1, C3, R, 4
A1, C3, R, 6
A1, C3, TS

If there is an existing implementation, is there any docs on how to use it?

Many Thanks
Allan


Oliver Wong

2006-09-18, 10:03 pm


"Allan M. Bruce" <allanmb@takeaway.dsl.pipex.com> wrote in message
news:OtidnUOUuY2eXZPYRVnyrQ@pipex.net...
>I am hoping to use a tree data structure in my Java application. I need to
>be able to create a tree which I can add next nodes to certain paths and
>obtain a list of all possible paths in the tree. Is there an
>implementation of this built into Java? For example, I would like to
>create a structure similar to this:
>
> A1
> / \
> M1 C3
> / | \ / \
> D S E R Ts
> /\
> 4 6
>
> Basically each node is labelled with a String, I dont want the data being
> sorted. And I want to iterate through all paths to get a list like:
>
> A1, M1, D
> A1, M1, S
> A1, M1, E
> A1, C3, R, 4
> A1, C3, R, 6
> A1, C3, TS
>
> If there is an existing implementation, is there any docs on how to use
> it?


AFAIK, there's nothing in Sun's class library like this. It should not
be very difficult to implement this yourself, though.

- Oliver

No Name

2006-09-18, 10:03 pm


Allan M. Bruce wrote:
> I am hoping to use a tree data structure in my Java application. I need to
> be able to create a tree which I can add next nodes to certain paths and
> obtain a list of all possible paths in the tree. Is there an implementation
> of this built into Java? For example, I would like to create a structure
> similar to this:
>
> A1
> / \
> M1 C3
> / | \ / \
> D S E R Ts
> /\
> 4 6


Ah, it's homework season. That time of the year where we get insight
into the state of programmer education.

Allan M. Bruce

2006-09-18, 10:03 pm


"No Name" <7abc@sogetthis.com> wrote in message
news:1158620713.452916.104890@b28g2000cwb.googlegroups.com...
>
> Allan M. Bruce wrote:
>
> Ah, it's homework season. That time of the year where we get insight
> into the state of programmer education.
>


Actually it is not! I could quite easily program this myself but thought I
would check before re-inventing the wheel. Looks like I will have to code
it myself from the feedback from some helpful people!
Allan


san1312@gmail.com

2006-09-19, 8:03 am


Allan M. Bruce wrote:
> "No Name" <7abc@sogetthis.com> wrote in message
> news:1158620713.452916.104890@b28g2000cwb.googlegroups.com...
>
> Actually it is not! I could quite easily program this myself but thought I
> would check before re-inventing the wheel. Looks like I will have to code
> it myself from the feedback from some helpful people!
> Allan


if you know GoF patterns, then you can do yourself.

Composite and Iterator pattern. I hope this will help

Babu Kalakrishnan

2006-09-20, 8:03 am

san1312@gmail.com wrote:
> Allan M. Bruce wrote:
>
>
>
> if you know GoF patterns, then you can do yourself.
>
> Composite and Iterator pattern. I hope this will help
>

Babu Kalakrishnan

2006-09-20, 8:03 am

Allan M. Bruce wrote:
> "No Name" <7abc@sogetthis.com> wrote in message
> news:1158620713.452916.104890@b28g2000cwb.googlegroups.com...
>
>
>
> Actually it is not! I could quite easily program this myself but thought I
> would check before re-inventing the wheel. Looks like I will have to code
> it myself from the feedback from some helpful people!



See if the javax.swing.DefaultMutableTreeNode is of any use. It does
provide methods that give you breadthFirst / depthFirst /preOrder
enumerations to traverse the tree as well as some utility method to find
paths between nodes.

BK
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com