public class ETNode2 { // Invariant of the BTNode class: // 1. Each node has one reference to an double Object, stored in the instance // variable data. // 2. The instance variables left and right are references to the node's // left and right children. private Object data; private ETNode2 left, right; public ETNode2(double initialData) { data = initialData; left = null; right = null; } public ETNode2(char initialOp, ETNode2 initialLeft, ETNode2 initialRight) { data = initialOp; left = initialLeft; right = initialRight; } /** * Accessor method to get the data from this node. * * @param - none * @return the data from this node **/ public double getData() { return (Double) data; } public char getOp() { return (Character) data; } /** * Accessor method to get a reference to the left child of this node. * * @param - none * @return a reference to the left child of this node (or the null reference * if there is no left child) **/ public ETNode2 getLeft() { return left; } /** * Accessor method to get a reference to the right child of this node. * * @param - none * @return a reference to the right child of this node (or the null * reference if there is no right child) **/ public ETNode2 getRight() { return right; } /** * Uses an inorder traversal to print the data from each node at or below * this node of the binary tree. * * @param - none
System.out.println( )
* using an inorder traversal.
**/
public void inorderPrint() {
if (left != null)
left.inorderPrint(); // 1
System.out.println(data);
if (right != null)
right.inorderPrint(); // 2
}
/**
* Uses a preorder traversal to print the data from each node at or below
* this node of the binary tree.
*
* @param - none System.out.println( )
* using a preorder traversal.
**/
public void preorderPrint() {
System.out.println(data);
if (left != null)
left.preorderPrint();
if (right != null)
right.preorderPrint();
}
/**
* Uses a postorder traversal to print the data from each node at or below
* this node of the binary tree.
*
* @param - none System.out.println( )
* using a postorder traversal.
**/
public void postorderPrint() {
if (left != null)
left.postorderPrint();
if (right != null)
right.postorderPrint();
System.out.println(data);
}
/**
* Uses an inorder traversal to print the data from each node at or below
* this node of the binary tree, with indentations to indicate the depth
* of each node.
* @param depth
* the depth of this node (with 0 for root, 1 for the root's
* children, and so on)(
* depth is the depth of this node.
* System.out.println( ) using an inorder traversal.
* The indentation of each line of data is four times its depth in the
* tree. A dash "--" is printed at any place where a child has no
* sibling.
**/
public void print(int depth) {
int i;
// Print the indentation and the data from the current node:
for (i = 1; i <= depth; i++)
System.out.print(" ");
if (isLeaf())
System.out.println((Double) data);
else
System.out.println((Character) data);
// Print the right subtree (or a dash if there is a left child and no
// left child)
if (right != null)
right.print(depth + 1);
else if (left != null) {
for (i = 1; i <= depth + 1; i++)
System.out.print(" ");
System.out.println("--");
}
// Print the left subtree (or a dash if there is a right child and no
// left child)
if (left != null)
left.print(depth + 1);
else if (right != null) {
for (i = 1; i <= depth + 1; i++)
System.out.print(" ");
System.out.println("--");
}
}
/**
* Accessor method to determine whether a node is a leaf.
* @param - none
* @return
* true (if this node is a leaf) or
* false (if this node is not a leaf.
**/
public boolean isLeaf() {
return (left == null) && (right == null);
}
/**
* Modification method to set the data in this node.
* @param newData
* the new data to place in this node
* newData.
**/
public void setData(double newData) {
data = newData;
}
public void setOp(char newOp) {
data = newOp;
}
/**
* Modification method to set the link to the left child of this node.
* @param newLeft
* a reference to the node that should appear as the left child of this node
* (or the null reference if there is no left child for this node)
* newLeft.
* Any other node (that used to be the left child) is no longer connected to
* this node.
**/
public void setLeft(ETNode2 newLeft) {
left = newLeft;
}
/**
* Modification method to set the link to the right child of this node.
* @param newLeft
* a reference to the node that should appear as the right child of this node
* (or the null reference if there is no right child for this node)
* newRight.
* Any other node (that used to be the right child) is no longer connected to
* this node.
**/
public void setRight(ETNode2 newRight) {
right = newRight;
}
public static double eval(ETNode2 t) {
if (t.isLeaf()) {
return (Double) t.data;
}
else
{
double op1 = eval(t.getLeft());
double op2 = eval(t.getRight());
if ((Character) t.data == '+') {
return op1 + op2;
}
else
{
return op1 * op2;
}
}
}
public double value() {
if (isLeaf())
return (Double) data;
else
{
double op1 = left.value();
double op2 = right.value();
if ((Character) data == '+')
return op1 + op2;
else
return op1 * op2;
}
}
}