• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Trees in Java

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a constructed a hash map with the following value .

The values of the hash map is another hash map

A - { B , C}
B - {D}
C - {D}
D - {E,F}

My output should be something like

A - B - D - E
A - B - D - F
A- C - D - E
A - C - D- F

What is the best way to find a solution to resolve this.
Any help in this regard is highly appreciated
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you explain in words (not Java) the process you want to follow? If you can do this, it should make the coding easier.
 
Shruthi Babu
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basicall I need to traverse the hash map to get the output I mentioned . I tried using recursive function but I am not able to get any solution.

Any help in this regard is really of great help
 
Sheriff
Posts: 22772
130
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would probably be something like this (I'm using Set here because it seems more appropriate):
 
Shruthi Babu
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the input . I see TreeNode is in swings. Can I use this approach for plain java code?
 
Shruthi Babu
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am also using Java 1.4.2 will this still work?
 
Rob Spoor
Sheriff
Posts: 22772
130
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Div Bala:
Thanks for the input . I see TreeNode is in swings. Can I use this approach for plain java code?


Sure you can. Although TreeNode is in the javax.swing.tree package, you can use it anywhere. It's still a tree, and you can traverse it using the children() enumeration and getParent().

Originally posted by Div Bala:
I am also using Java 1.4.2 will this still work?


Sure, you just need to remove the generics and do some casting instead, and use an iterator to iterate through the set:
 
Shruthi Babu
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the help.

I am having difficulty in understanding the logic say for example

I have something like
A - B,C
B - E
C- E

How will the tree maintain all the possible combinations as A - B - E
A-C- E ?

Sorry to bother you with questions.
 
Rob Spoor
Sheriff
Posts: 22772
130
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Div Bala:
Thanks for the help.

I am having difficulty in understanding the logic say for example

I have something like
A - B,C
B - E
C- E

How will the tree maintain all the possible combinations as A - B - E
A-C- E ?

Sorry to bother you with questions.


You start the tree with object A; this will be the root node.
A will have two children: B and C. Each of these has one child, E. So the tree will look like this:

If you now traverse the tree, storing the entire path, you will get both A-B-E and A-C-E.
 
Shruthi Babu
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok got it. Thanks.

Can you help me with the traversal part? Again should I write a recursive function?

What if there are two root nodes say like

A - C , D
B - C , D

Thanks
Rohini
 
Rob Spoor
Sheriff
Posts: 22772
130
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Div Bala:
Can you help me with the traversal part? Again should I write a recursive function?


Yes you should.


Initially path is an empty list when you call it with the root node.

What if there are two root nodes say like

A - C , D
B - C , D


Then you have to call the method(s) for both nodes.

All recursive tree operations have two assumptions:
1) there is one root
2) there are no cycles


1 because you always start at the root, and 2 because cycles could (will) cause your code to run forever - until you get either a StackOverFlowError or an OutOfMemoryError.
 
Shruthi Babu
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for all your help.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm with Rob on the Two or more Root Node thing

There is always one Root Node in a Tree, sometimes Users (abusers) try to make you implement a solution where one tree consists of many trees (for whatever reason), in that instance you can have a list of lists, where a list contains a list of One Root Node Trees. Anyway if you don't want to use Swing it easy enough to write your one Tree Solution, just remember you will have to write classes on top to implement you rules

 
Did you just should on me? You should read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic