Reputation: 11
I'm in a bit of a trouble: I built a Binary Tree class with some functions in it which are not interesting for the purpose of this task. Every node stores a Left and Right Child plus a Parent. Every node has a key value which can be seen as a label. I created 3 classes:
Tree.java is the class of a node.
BinaryTree.java contains some methods relevant for the whole tree like min/max value.
Main.java which tests features of the Tree and contains the main method.
MY PROBLEM: I wanted to write a functional Interface with a method which get's a node as a parameter and outputs a boolean value. This could be used to pass in the Root node and check recursevely if every node in the tree is greater or less than a value. However I am completely new to Functional Interfaces and I can't really grasp the logic behind the functionality. This is what I got so far:
@FunctionalInterface
public interface NodeOperation {
public abstract boolean forAll(Tree node);
}
NodeOperation overTwenty = (node) -> node.getValue() < 20;
When I try the lambda notation it doesn't return me a boolean when I want print overTwenty. Can someone help me realize the functional Interface and explain me how I access the boolean variable so I can start thinking about how I do this recursevely for every node.
If you don't know much about Binary Trees I suggest you to look it up on Wikipedia. In my case I made a BST(Binary Search Tree) meaning it's structure is based on bigger values being on the right and smaller values being on the left. If you need some specific part of my code just suggest it and I will post it here :)
Upvotes: 0
Views: 625
Reputation: 41
In java the methods of the interfaces are always public. So your Interface can become
@FuctionalInterface
public interface NodeOperation {
boolean forAll(Tree node);
}
So you have written this line of code
NodeOperation overTwenty = (node) -> node.getValue() < 20;
Wich creates you an instance of the interface that checks if the value of a node is lower than 20
So suppose that you have an instance of Tree node with the value 30 If you call
overTwenty.forAll(node) //will return false
This function is not recursive. If you want to apply the function to all children of node you have to write a recursive method on the Tree class
public class Tree{
...
public boolean recursiveNodeOperation(NodeOperation operation) {
if(!operation.forAll(this)) return false;
for(Tree child : children)
if(! child.recursiveNodeOperation(operation))
return false
return true ;
}
}
root.recursiveNodeOperation(overTwenty); //will return true if all the nodes of the Tree starting from root are lower than 20
This method will apply the Node operation recursively so will check that all the elements in the Tree matches your function
Upvotes: 1
Reputation: 2922
The overTwenty
object you created is a function. If you want to use it in a node of your tree, you have to call its only method on a node of your tree. For example you can call it that way:
boolean result = overTwenty.forAll(root);
By the way, you NodeOperation
interface is quite equivalent to a Function<Tree, Boolean>
with the small difference that it returns a primitive boolean
instead of the class Boolean
.
Upvotes: 0