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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript 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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Tree with parents