my dog learned polymorphism*
The moose likes Java in General and the fly likes Tree Data Structure Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Tree Data Structure" Watch "Tree Data Structure" New topic
Author

Tree Data Structure

Shine Tom
Greenhorn

Joined: Sep 17, 2012
Posts: 19
Hello,
Can somebody please help me with my Tree implementation. I have an array list with values("1.1","1.2","1.3","1.3.1.1","1.3.1.2","1.4","1.4.1.1","1.4.2.1","1.4.2.1.1.1","1.4.2.1.1.2","1.5","1.6","1.6.1.1") and I m trying to create a Tree with this data. Please find below attached my code for the same.

My tree should look like this in the picture attached.


Now I want to print the parent of each node starting from the last one, so starting at "1.6.1.1" . and but dont know to to traverse the branch 1.4.1.1 and other children of 1.4 children. Please someone help me.
PS: I am new to java so please correct me if tehre is something wrong.


[Thumbnail for Unbenannt12_1.jpg]

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37950
    
  22
It looks as if you have been guessing, and have found a class with a promising name and not looked up what it is there for, namely the tree node class.

You will have to start from scratch. Start by learning what a tree structure is. It is probably easiest to implement with an inner class which is a Node, having lfet right and value fields. That means the objects inserted must have some sort of sorting, which you can find out about in the Java Tutorials.
Shine Tom
Greenhorn

Joined: Sep 17, 2012
Posts: 19
Hi Campbell,
what is said is true. I m not really familiar with java and also the Data Structure concept behind that. I m in the process of learning that by doing.
In my tree i can have n number of children ideally, not just a left or right. that is why i didnt try to make a Node class. or otherwise I should make a List<Node> children, which i was not so sure of..
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2231
    
    7

You should start small take one step at a time. Instead of seeing the tree as many levels or nodes, assuming there's one or two and get that to work first. From there, extend it.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37950
    
  22
Agree. But once you have a 1‑level tree working, you can fill it with as many levels as you like really easily, because it has a recursive or fractal structure.
Get a common‑or‑garden binary tree working, then you can consider how to enhance it to that variable arity tree. That variable arity is unusual, so you cannot implement it unless you think very carefully about how you determine which value belongs at which level. You need very precise rules about how to achieve that.
 
jQuery in Action, 2nd edition
 
subject: Tree Data Structure
 
Similar Threads
Tree Data Structure
I am lost , Help Me Please ...JTree
Accessing image files of user entered paths
how to check parent availability in Jtree structure in swing
In your lab05, replace instructor’s Tokenizer class and MyStackQueue package with your own.