• 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
  • 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

Tree Data Structure

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Marshal
Posts: 79178
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79178
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic