1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
/*
* Tree.java
*
* Created on 20 septembre 2006, 17:25
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package evaluator;
import java.io.Serializable;
import java.util.ArrayList;
/**
* This class models a node, component of a tree created by recursive call.
* A node has a father unless it is the first element of the tree, and some
* children unless it is a leaf (one of the lasts elements of the tree). A node
* also contains data witch are significative for itself, and and operation to
* apply between the children, e.g. +,* ,^, &&, and so on.
* @see evaluator.Evaluator
* @see evaluator.Expression
* @see evaluator.Term
* @see evaluator.Factor
* @author Absil Romain
*/
public class Node implements Serializable
{
private String operation;
private Node father;
private ArrayList<Node> children;
private Object data;
/**
* Creates an instance of Node.
* @param operation the operation associated to the node.
* @param father the father of the node, enter null if it id the root of
* the tree.
* @param data the significative contained data.
**/
public Node(String operation, Node father, Object data)
{
this.operation = operation;
this.father = father;
children = new ArrayList<Node>();
this.data = data;
}
/**
* Returns the operation asociated to the node.
* @return the operation asociated to the node.
**/
public String getOperation()
{
return operation;
}
/**
* Returns the father of the node, return null if it is the root of the tree.
* @return the father of the node, return null if it is the root of the tree.
**/
public Node getFather()
{
return father;
}
/**
* Returns the children of the node; a void {@link java.util.ArrayList} if
* it is a leaf.
* @return the children of the node; a void {@link java.util.ArrayList} if
* it is a leaf.
**/
public ArrayList<Node> getChildren()
{
return children;
}
/**
* Returns the data of the node.
* @return the data of the node.
**/
public Object getData()
{
return data;
}
/**
* Sets the data of the node.
* @param data the new data.
**/
public void setData(Object data)
{
this.data = data;
}
/**
* Returns true if the node is the root, i.e. if its father is null, returns
* false otherwise.
* @return true if the node is the root, i.e. if its father is null, returns
* false otherwise.
**/
public boolean isRoot()
{
return father == null;
}
/**
* Returns true if the node is a leaf, i.e. if it has no children.
* @return true if the node is a leaf, i.e. if it has no children.
**/
public boolean isLeaf()
{
return children.size() == 0;
}
} |
Partager