This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Tree data with multiple top nodes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Tree data with multiple top nodes" Watch "Tree data with multiple top nodes" New topic
Author

Tree data with multiple top nodes

John Jai
Rancher

Joined: May 31, 2011
Posts: 1776
Hi,

I would like to represent a (or many) Tree that may have more than one top node and children beneath them. All content is of String datatype and the end application is a Swing application where users should be shown the hierarchy of a selected top node in a tree structure.

E.g. data would be like

Root 1 Root 2

Child1 Child2 Child3 Child4

GrandChild 21 GrandChild 31 GrandChild 41
GrandChild 22 GrandChild 32

so if user clicks a Root 2... then i should be able to show him Root2's hierarchy... i.e. Root 2 has Child 3 & Child 4... & Child 3 has GrandChild31... so on


Currently I have thought to use the all the Roots as a separate list and a single hashmap for parent child mapping. So if a particular root is asked i will traverse the hashmap and print all its children. Is the Collection structure I use good?

<edit>Sorry that the alignment of Root and Child in the quote is not spaced up properly like i gave. Root2 has Child3 and Child4 in it..</edit>
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 4087
    
  18

So really you're just using multiple trees? Why not use a List<MyNode>?


The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
John Jai
Rancher

Joined: May 31, 2011
Posts: 1776
What are MyTree<MyNode>. Custom class?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 4087
    
  18

Sorry, I edited my post after you read it. I removed MyTree, because nodes themselves are of course the start of a tree.

MyNode would be a custom class, indeed. You can give it other MyNode children, and it will store a generic value.
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

You should look up 2-3 Trees


Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
John Jai
Rancher

Joined: May 31, 2011
Posts: 1776
Thanks Stephen & Hunter - I expect each of the top nodes to have at the least of 1000 children. So was worried if implementing each node as a custom class object might hit the performance. Another option I got from searching other forums is just use a JTree given by Swing. But I dont know how a Swing view implementation like JTree would suffice the requirement. I will try that too.

Thanks again!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Tree data with multiple top nodes
 
It's not a secret anymore!