• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to create dynamic tree data structure in Java  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Specifically I need to represent the following:

1. The tree at any node can have an arbitrary number of children
2. Each parent node (after the root) is just a String (whose children are also Strings)
3. I need to be able to get parent and list out all the children (some sort of list or array of Strings) given an input string representing a given node
4. Dynamically populating the tree structure based on reference relationship between parent and child.
Example given is I have one member1 sponsor another member2, and member2 sponsor member 3 and so and so for. Already have the table records relationship

Is there an available structure for this ???

The closest code I found is this but this is not dynamic.
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

What makes you say that it's not dynamic? Also, if this is for schoolwork, are you allowed to use someone else's work or use an implementation from the standard Java libraries?
 
vincent johnson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
only implementation from java libraries , Java 7 or Java 8
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, but you didn't answer the more important question: What makes you say that code you cited isn't dynamic?

The example usage obviously isn't dynamic but that's just a simple example. The TreeNode class given can definitely be used in a more dynamic manner.
 
vincent johnson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
let say my data is from DB or a List, I will loop through the information with the name and the relation to determine if the node is a root, parent or a child.

So during the loop, I found a child, I need a reference to the parent so that I can compare the child relation to the parent before adding the child to its parent.




   
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok...

From what I can deduce about your data, that code won't work. What is your question?
 
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
TreeNode has a getParent() method.  Can you use this?
 
vincent johnson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question is how can I solve my current issue , looping through the current entry at the same time able to reference to the parent and add it as a child dynamically.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you need to walk through the logic manually to lay out the procedure for building up your tree. I don't know how your data is arranged but assuming no particular order, there are at least two different operations you need to perform: 1. add a child and 2. traverse the tree to find a node.

Don't try to solve this in one method. Figure out how to break the problem down into smaller, logical chunks. When you know how to do each of the smaller tasks, then it's easier to put them all together to complete the larger task.

@Knute, what you referenced is a method in a Swing interface. I don't think that's particularly appropriate in OP's case.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if you're wondering about my comment about the order of your tree data, it does make a difference.  If the data was ordered in such a way that you could build the tree from the root node downwards, then the algorithm for building up the tree would be pretty straightforward because you're going to be guaranteed to always find the parent node on which addChild() should be called.  However, if the data is arranged in no particular order, then it's possible that you could read a child node before you've had a chance to add its parent node to the tree. That complicates the tree building algorithm a little bit. Again, you need to walk through the whole tree building process manually, ideally with a pencil/pen and paper, to figure out the different tasks you need to perform. Only when you understand the process this way will you be able to write the proper instructions for the computer to do it for you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!