Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Tree Data Structure

 
Shine Tom
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Unbenannt12_1.jpg
[Thumbnail for Unbenannt12_1.jpg]
 
Campbell Ritchie
Sheriff
Pie
Posts: 49756
69
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3502
14
Android Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49756
69
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic