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
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Trees in Java

Ranch Hand
Posts: 54
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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: 22784
131
• Number of slices to send:
Optional 'thank-you' note:
It would probably be something like this (I'm using Set here because it seems more appropriate):

Shruthi Babu
Ranch Hand
Posts: 54
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
I am also using Java 1.4.2 will this still work?

Rob Spoor
Sheriff
Posts: 22784
131
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
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: 22784
131
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
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: 22784
131
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
Thank you very much for all your help.

Greenhorn
Posts: 19
• Number of slices to send:
Optional 'thank-you' note:
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

 Beware the other head of science - it bites! Nibble on this message: a bit of art, as a gift, that will fit in a stocking https://gardener-gift.com