com.epochx.representation
Class Node<TYPE>

java.lang.Object
  extended by com.epochx.representation.Node<TYPE>
All Implemented Interfaces:
java.lang.Cloneable
Direct Known Subclasses:
FunctionNode, TerminalNode

public abstract class Node<TYPE>
extends java.lang.Object
implements java.lang.Cloneable

Subclasses of Node should ensure they call the superclass constructor with all child Nodes so information such as the arity of the Node can be maintained. Concrete subclasses must also implement evaluate(). A Node is a vertex in a tree structure which represents a program. A Node can be thought of as an expression in a computer programming language. Evaluating a Node will involve evaluating any children and potentially performing some operation and/or returning a value. A program is maintained by the CandidateProgram class.

See Also:
FunctionNode, TerminalNode

Constructor Summary
Node(Node<TYPE>... children)
          Node constructor.
 
Method Summary
 java.lang.Object clone()
          Create a deep copy of this node tree.
 boolean equals(java.lang.Object obj)
          Compare an object for equality.
abstract  TYPE evaluate()
          Performs some operation and/or returns a value associated with this Node.
 int getArity()
          Returns the number of immediate children this Node has.
 Node<TYPE> getChild(int index)
          Returns a specific child by index.
 Node<TYPE>[] getChildren()
          Returns an array of the children of this node.
 int getDepth()
          Returns the depth of deepest node in the node tree, given that this node is at depth zero.
 java.util.List<FunctionNode<TYPE>> getFunctionNodes()
          Returns a list of all the function nodes in the this node tree.
 int getLength()
          Returns the number of nodes in the node tree.
 java.util.List<Node<TYPE>> getNodesAtDepth(int depth)
          Retrieves all the nodes in the node tree at a specified depth from this current node.
 int getNoDistinctFunctions()
          Returns a count of how many unique function nodes are in the node tree.
 int getNoDistinctTerminals()
          Returns a count of how many unique terminal nodes are in the node tree.
 int getNoFunctions()
          Returns a count of how many function nodes are in the node tree.
 int getNoTerminals()
          Returns a count of how many terminal nodes are in the node tree.
 Node<TYPE> getNthNode(int n)
          Returns the element at the specified position in the node tree, where this node is considered to be the root - that is the 0th node.
 java.util.List<TerminalNode<TYPE>> getTerminalNodes()
          Returns a list of all the terminal nodes in the this node tree.
 int hashCode()
           
 void setChild(int index, Node<TYPE> child)
          Replaces the child node at the specified index with the specified node.
 void setChildren(Node<TYPE>[] children)
          Sets the children of this node.
 void setNthNode(int n, Node<TYPE> newNode)
          Replaces the node at the specified position in this node tree, where this node is considered to be the root node - that is, the 0th node.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Node

public Node(Node<TYPE>... children)
Node constructor.

Parameters:
children - child nodes to this node.
Method Detail

evaluate

public abstract TYPE evaluate()
Performs some operation and/or returns a value associated with this Node.

Returns:
The result of evaluating the candidate program.

getChildren

public Node<TYPE>[] getChildren()
Returns an array of the children of this node.

Returns:
the children of this node.

setChildren

public void setChildren(Node<TYPE>[] children)
Sets the children of this node.

Parameters:
children - the new children to be set.

getChild

public Node<TYPE> getChild(int index)
Returns a specific child by index.

Parameters:
index - the index (representing arity) of the child to be returned, indexes are from zero.
Returns:
the child node at the specified arity index.

getNthNode

public Node<TYPE> getNthNode(int n)
Returns the element at the specified position in the node tree, where this node is considered to be the root - that is the 0th node. The tree is traversed in pre-order (depth first).

Parameters:
n - the index of the node to be returned.
Returns:
the node at the specified position in this node tree.

setNthNode

public void setNthNode(int n,
                       Node<TYPE> newNode)
Replaces the node at the specified position in this node tree, where this node is considered to be the root node - that is, the 0th node. The tree is traversed in pre-order (depth first).

Parameters:
n - the index of the node to replace.
newNode - the node to be stored at the specified position.

getNodesAtDepth

public java.util.List<Node<TYPE>> getNodesAtDepth(int depth)
Retrieves all the nodes in the node tree at a specified depth from this current node. This node is considered to be depth zero.

Parameters:
depth - the specified depth of the nodes.
Returns:
a List of all the nodes at the specified depth.

setChild

public void setChild(int index,
                     Node<TYPE> child)
Replaces the child node at the specified index with the specified node.

Parameters:
index - the index of the child to replace within the nodes arity.
child - the child node to be stored at the specified position.

getArity

public int getArity()
Returns the number of immediate children this Node has.

Returns:
the number of child Nodes to this Node.

getNoTerminals

public int getNoTerminals()
Returns a count of how many terminal nodes are in the node tree.

Returns:
the number of terminal nodes in this node tree.

getNoDistinctTerminals

public int getNoDistinctTerminals()
Returns a count of how many unique terminal nodes are in the node tree.

Returns:
the number of unique terminal nodes in this node tree.

getTerminalNodes

public java.util.List<TerminalNode<TYPE>> getTerminalNodes()
Returns a list of all the terminal nodes in the this node tree.

Returns:
a List of all the terminal nodes in the node tree.

getNoFunctions

public int getNoFunctions()
Returns a count of how many function nodes are in the node tree.

Returns:
the number of function nodes in this node tree.

getNoDistinctFunctions

public int getNoDistinctFunctions()
Returns a count of how many unique function nodes are in the node tree.

Returns:
the number of unique function nodes in this node tree.

getFunctionNodes

public java.util.List<FunctionNode<TYPE>> getFunctionNodes()
Returns a list of all the function nodes in the this node tree.

Returns:
a List of all the function nodes in the node tree.

getDepth

public int getDepth()
Returns the depth of deepest node in the node tree, given that this node is at depth zero.

Returns:
the depth of the deepest node in the node tree.

getLength

public int getLength()
Returns the number of nodes in the node tree.

Returns:
the number of nodes in the node tree.

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

clone

public java.lang.Object clone()
Create a deep copy of this node tree. Each child node will be cloned.

Overrides:
clone in class java.lang.Object
Returns:
a copy of this Node.

equals

public boolean equals(java.lang.Object obj)
Compare an object for equality. If the given object is a Node then it may be equal if each Node in the tree is equal. Equality of individual Nodes is dependant on the specific node type but typically will be whether they are the same type and have the same children for function nodes and whether they have the same value or are the same variable for terminal nodes.

Overrides:
equals in class java.lang.Object
Parameters:
obj - an object to be compared for equivalence.
Returns:
true if this node tree is the same as the obj argument; false otherwise.