how can i do crossover in this tree structure?
hi there! I have these classes which model a tree (binary). the thing is i cant figure out how i can set the elements of individual nodes in that tree. i can get individual elements but i cannot set them due to the strucutre of the tree and how it is implemented. The changes that I make to these classes should be as less as possible, because i have an algorithm which generates the tree structure randomly, plus i can evaluate the tree easily by using recursion. The only left thing to do is CROSSOVER but how?!?
here are the classes which model my binary tree:
Code:
public abstract class Node implements Cloneable{
abstract double evaluate(VariableInput v);
abstract String print();
abstract int getNumberOfNodes();
abstract ArrayList<Object> getChildren();
@Override
public Node clone(){
Node copy;
try {
copy = (Node) super.clone();
} catch (CloneNotSupportedException unexpected) {
throw new AssertionError(unexpected);
}
//In an actual implementation of this pattern you might now change references to
//the expensive to produce parts from the copies that are held inside the prototype.
return copy;
}
}
Code:
public class UnaryNode extends Node {
private UnaryFunction operator;
private Node left;
public UnaryNode(UnaryFunction op, Node terminal) {
operator = op;
this.left = terminal;
}
public String print(){
String r = "(" + operator.toString()+ " " + left.print() + ")";
return r;
}
void setLeft(Node left) {
this.left = left;
}
@Override
int getNumberOfNodes() {
return 1 + left.getNumberOfNodes();
}
Node getLeft() {
return left;
}
ArrayList<Object> getChildren() {
ArrayList<Object> arr = new ArrayList<Object>();
arr.add(this);
arr.addAll(left.getChildren());
return arr;
}
Code:
public class BinaryNode extends Node {
private BinaryFunction operator;
private Node left;
private Node right;
public BinaryNode(BinaryFunction op, Node left, Node right) {
operator = op;
this.left = left;
this.right = right;
}
public String print(){
String r = "(" + operator.toString()+ " " + left.print() + " " + right.print()+")";
return r;
}
public void setLeft(Node left){
this.left = left;
}
public void setRight(Node right){
this.right = right;
}
@Override
int getNumberOfNodes() {
return 1 + left.getNumberOfNodes() + right.getNumberOfNodes();
}
Node getRight() {
return right;
}
Node getLeft() {
return left;
}
@Override
ArrayList<Object> getChildren() {
ArrayList<Object> arr = new ArrayList<Object>();
arr.add(this);
arr.addAll(left.getChildren());
arr.addAll(right.getChildren());
return arr;
}
Code:
public class NumericNode extends Node{
private double value;
public NumericNode(double v){
value = v;
}
@Override
double evaluate(VariableInput c) {
return value;
}
public String print(){
String r = "" + value;
return r;
}
@Override
int getNumberOfNodes() {
return 1;
}
@Override
ArrayList<Object> getChildren() {
ArrayList<Object> arr = new ArrayList<Object>();
arr.add(new NumericNode(value));
return arr;
}
p.s. I have this get children method which return a list of REFERENCES to all the nodes in the tree, but if i change any of them it wont have an effect to the tree itself because they are references.
Any ideas or codes will be much appreciated. Thanks!