Results 1 to 3 of 3
  1. #1
    player123 is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Red face 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:

    Java 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;
        }
    }
    Java 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;
        }
    Java 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;
        }
    Java 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!

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    i think i understand what you're asking. you have to change the reference to the child in the parent node. you can always just change the properties of the child node, too.

  3. #3
    player123 is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by emceenugget View Post
    i think i understand what you're asking. you have to change the reference to the child in the parent node. you can always just change the properties of the child node, too.
    how..? i dont understand. can you give an example please? thank you!

Similar Threads

  1. Tree structure using JAVA
    By trill in forum Advanced Java
    Replies: 2
    Last Post: 05-27-2010, 12:02 PM
  2. Replies: 1
    Last Post: 10-20-2008, 01:58 PM
  3. Printing Tree Structure using Swings
    By pradeep1_mca@yahoo.com in forum AWT / Swing
    Replies: 5
    Last Post: 08-30-2008, 02:54 PM
  4. Use if then else structure, help
    By paul in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:00 AM
  5. Help with Structure
    By Albert in forum New To Java
    Replies: 1
    Last Post: 07-13-2007, 04:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •