• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Tree data with multiple top nodes

 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5334
48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So really you're just using multiple trees? Why not use a List<MyNode>?
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are MyTree<MyNode>. Custom class?
 
Stephan van Hulst
Bartender
Posts: 5334
48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should look up 2-3 Trees


Hunter
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic