aspose file tools*
The moose likes Beginning Java and the fly likes Trees in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trees in Java" Watch "Trees in Java" New topic
Author

Trees in Java

Shruthi Babu
Ranch Hand

Joined: May 04, 2007
Posts: 54
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
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Can you explain in words (not Java) the process you want to follow? If you can do this, it should make the coding easier.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Shruthi Babu
Ranch Hand

Joined: May 04, 2007
Posts: 54
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
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

It would probably be something like this (I'm using Set here because it seems more appropriate):


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Shruthi Babu
Ranch Hand

Joined: May 04, 2007
Posts: 54
Thanks for the input . I see TreeNode is in swings. Can I use this approach for plain java code?
Shruthi Babu
Ranch Hand

Joined: May 04, 2007
Posts: 54
I am also using Java 1.4.2 will this still work?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

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

Joined: May 04, 2007
Posts: 54
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

Joined: Oct 27, 2005
Posts: 19543
    
  16

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

Joined: May 04, 2007
Posts: 54
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

Joined: Oct 27, 2005
Posts: 19543
    
  16

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

Joined: May 04, 2007
Posts: 54
Thank you very much for all your help.
Tom Bigbee
Greenhorn

Joined: Sep 16, 2002
Posts: 19
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



Thomas Bigbee - SCJP, SCJD, SCWCD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trees in Java
 
Similar Threads
what are you learning/reading these days?
A simple Hash Map Example.
Having trouble choosing a data structure
<<<EOF in javascript?
Dan's question