I'm trying to find a way that I could take a binary tree class and traverse through its nodes,
performing X amount of inline actions on each node without having to rewrite the same traversal code over and over again.
I'd imagine if Java allowed function pointers, this would be much simpler for me to figure out...
Basically, what I need is something like the following:
public class BinaryTreeNode {
//...
public void inOrderTraversalFrom(BinaryTreeNode node, /* ??? */ actions) {
if(node.left != null)
inOrderTraversalFrom(node.left);
/* do something with "actions" here */
if(node.right != null)
inOrderTraversalFrom(node.right);
}
}
... where actions could allow for different methods to be performed, taking in a different set of parameters depending on the type of action(s) to be performed on each node.
A good example of this would be a class which is meant to draw these nodes could be passed, accepting a Graphics object as one of it's parameters, versus a class which is meant to perform some other series of actions which wouldn't require a Graphics object as a parameter, but a completely different set of parameters instead.
How is this possible? What would be the most dynamic way to perform this?