wood burning stoves 2.0*
The moose likes Java in General and the fly likes Tree with parents Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Tree with parents" Watch "Tree with parents" New topic
Author

Tree with parents

John Vorwald
Ranch Hand

Joined: Sep 26, 2010
Posts: 139
Hi,
I have a tree structure that uses a list of nodes for the children, along with a reference to the parent.
I would like to be able to extract a node incorporating the data from the ancestors of a node.
But, I don't know how to call a member of the generic class.





I have an alternative solution where the generic class contains a reference to a tree node, and can access the parent, so the generic class can construct the summary.
In the alternative solution, I don't get the expected result of "Beginning of the week", 6. The output is "week", 3.

John Vorwald
Ranch Hand

Joined: Sep 26, 2010
Posts: 139
The alternative solution had a error in the incorporateParent member. The correction is below.
But, I'd still prefer to use the first alternative, to have the tree/node class manage incorporating ancestors data, instead of passing the tree node to the generic class.

John Vorwald
Ranch Hand

Joined: Sep 26, 2010
Posts: 139
I dug a little deeper into how the treenode class could be modified to manage the incorporating of ancestors. One approach to allow a generic function to call a specific member function is to make the generic extend an interface that defines the function.

The following code is an example of a tree that is able to incorporate data from ancestor nodes. It produces the result "Beginning of the week", 6, which is the desired result.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Tree with parents
 
Similar Threads
traversing a simple tree in js.
lists inside a list :(((
Serialization, Heap and Objects
Deadlock scenario
Doubt regarding a method call.