• Post Reply Bookmark Topic Watch Topic
  • New Topic

Tree with parents  RSS feed

 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!