Answered! Please respond to both questions to a give code A) Complete the evaluate(double x) method, which returns the value of…

 

Please respond to both questions to a give code

A) Complete the evaluate(double x) method, which returns the value of
the expression rooted at the treeNode on which it called, for a particular
value of x: For example, evaluate(1) on the tree above should return
approximately 2.01. . . . For sin and cos, angles are measured in radians.

B) [Bonus, 30%] Complete the di erentiate() method, which returns a
new expression tree describing the derivative of the expression described
by the tree rooted at the treeNode on which it is called. Notice that
the original tree should remain intact and that the tree containing the
derivative should share no node with the original.

Here is the given code!

import java.lang.Math.*;

class expressionTreeNode {
private String value;
private expressionTreeNode leftChild, rightChild, parent;

expressionTreeNode() {
value = null;
leftChild = rightChild = parent = null;
}

// Constructor
/* Arguments: String s: Value to be stored in the node
expressionTreeNode l, r, p: the left child, right child, and parent of the node to created
Returns: the newly created expressionTreeNode
*/
expressionTreeNode(String s, expressionTreeNode l, expressionTreeNode r, expressionTreeNode p) {
value = s;
leftChild = l;
rightChild = r;
parent = p;
}

/* Basic access methods */
String getValue() { return value; }

expressionTreeNode getLeftChild() { return leftChild; }

expressionTreeNode getRightChild() { return rightChild; }

expressionTreeNode getParent() { return parent; }

/* Basic setting methods */
void setValue(String o) { value = o; }

// sets the left child of this node to n
void setLeftChild(expressionTreeNode n) {
leftChild = n;
n.parent = this;
}

// sets the right child of this node to n
void setRightChild(expressionTreeNode n) {
rightChild = n;
n.parent=this;
}
// Returns the root of the tree describing the expression s
// Watch out: it makes no validity checks whatsoever!
expressionTreeNode(String s) {
// check if s contains parentheses. If it doesn’t, then it’s a leaf
if (s.indexOf(“(“)==-1) setValue(s);
else { // it’s not a leaf

/* break the string into three parts: the operator, the left operand,
and the right operand. ***/
setValue( s.substring( 0 , s.indexOf( “(” ) ) );
// delimit the left operand 2008
int left = s.indexOf(“(“)+1;
int i = left;
int parCount = 0;
// find the comma separating the two operands
while (parCount>=0 && !(s.charAt(i)==’,’ && parCount==0)) {
if ( s.charAt(i) == ‘(‘ ) parCount++;
if ( s.charAt(i) == ‘)’ ) parCount–;
i++;
}
int mid=i;
if (parCount<0) mid–;

// recursively build the left subtree
setLeftChild(new expressionTreeNode(s.substring(left,mid)));

if (parCount==0) {
// it is a binary operator
// find the end of the second operand.F13
while ( ! (s.charAt(i) == ‘)’ && parCount == 0 ) ) {
if ( s.charAt(i) == ‘(‘ ) parCount++;
if ( s.charAt(i) == ‘)’ ) parCount–;
i++;
}
int right=i;
setRightChild( new expressionTreeNode( s.substring( mid + 1, right)));
}
}
}

// Returns a copy of the subtree rooted at this node… 2014
expressionTreeNode deepCopy() {
expressionTreeNode n = new expressionTreeNode();
n.setValue( getValue() );
if ( getLeftChild()!=null ) n.setLeftChild( getLeftChild().deepCopy() );
if ( getRightChild()!=null ) n.setRightChild( getRightChild().deepCopy() );
return n;
}

// Returns a String describing the subtree rooted at a certain node.
public String toString() {
String ret = value;
if ( getLeftChild() == null ) return ret;
else ret = ret + “(” + getLeftChild().toString();
if ( getRightChild() == null ) return ret + “)”;
else ret = ret + “,” + getRightChild().toString();
ret = ret + “)”;
return ret;
}

// Returns the value of the expression rooted at a given node
// when x has a certain value
double evaluate(double x) {
// WRITE YOUR CODE HERE

// AND CHANGE THIS RETURN STATEMENT
return 0;
}

/* returns the root of a new expression tree representing the derivative of the
original expression */
expressionTreeNode differentiate() {
// WRITE YOUR CODE HERE

// AND CHANGE THIS RETURN STATEMENT
return null;
}

public static void main(String args[]) {
expressionTreeNode e = new expressionTreeNode(“mult(add(2,x),cos(x))”);
System.out.println(e);
System.out.println(e.evaluate(1));
System.out.println(e.differentiate());

}
}

Expert Answer

 import java.lang.Math.*;

class expressionTreeNode {

private String value;
private expressionTreeNode leftChild, rightChild, parent;

expressionTreeNode() {
value = null;
leftChild = rightChild = parent = null;
}

// Constructor
/* Arguments: String s: Value to be stored in the node
expressionTreeNode l, r, p: the left child, right child, and parent of the node to created
Returns: the newly created expressionTreeNode
*/
expressionTreeNode(String s, expressionTreeNode l, expressionTreeNode r, expressionTreeNode p) {
value = s;
leftChild = l;
rightChild = r;
parent = p;
}

/* Basic access methods */
String getValue() {
return value;
}

expressionTreeNode getLeftChild() {
return leftChild;
}

expressionTreeNode getRightChild() {
return rightChild;
}

expressionTreeNode getParent() {
return parent;
}

/* Basic setting methods */
void setValue(String o) {
value = o;
}

// sets the left child of this node to n
void setLeftChild(expressionTreeNode n) {
leftChild = n;
n.parent = this;
}

// sets the right child of this node to n
void setRightChild(expressionTreeNode n) {
rightChild = n;
n.parent = this;
}

// Returns the root of the tree describing the expression s
// Watch out: it makes no validity checks whatsoever!
expressionTreeNode(String s) {
// check if s contains parentheses. If it doesn’t, then it’s a leaf
if (s.indexOf(“(“) == -1) {
setValue(s);
} else { // it’s not a leaf
/* break the string into three parts: the operator, the left operand,
and the right operand. ***/
setValue(s.substring(0, s.indexOf(“(“)));
// delimit the left operand 2008
int left = s.indexOf(“(“) + 1;
int i = left;
int parCount = 0;
// find the comma separating the two operands
while (parCount >= 0 && !(s.charAt(i) == ‘,’ && parCount == 0)) {
if (s.charAt(i) == ‘(‘) {
parCount++;
}
if (s.charAt(i) == ‘)’) {
parCount–;
}
i++;
}
int mid = i;
if (parCount < 0) {
mid–;
}
// recursively build the left subtree
setLeftChild(new expressionTreeNode(s.substring(left, mid)));

if (parCount == 0) {
// it is a binary operator
// find the end of the second operand.F13
while (!(s.charAt(i) == ‘)’ && parCount == 0)) {
if (s.charAt(i) == ‘(‘) {
parCount++;
}
if (s.charAt(i) == ‘)’) {
parCount–;
}
i++;
}
int right = i;
setRightChild(new expressionTreeNode(s.substring(mid + 1, right)));
}
}
}

// Returns a copy of the subtree rooted at this node… 2014
expressionTreeNode deepCopy() {
expressionTreeNode n = new expressionTreeNode();
n.setValue(getValue());
if (getLeftChild() != null) {
n.setLeftChild(getLeftChild().deepCopy());
}
if (getRightChild() != null) {
n.setRightChild(getRightChild().deepCopy());
}
return n;
}

// Returns a String describing the subtree rooted at a certain node.
public String toString() {
String ret = value;
if (getLeftChild() == null) {
return ret;
} else {
ret = ret + “(” + getLeftChild().toString();
}
if (getRightChild() == null) {
return ret + “)”;
} else {
ret = ret + “,” + getRightChild().toString();
}
ret = ret + “)”;
return ret;
}

// Returns the value of the expression rooted at a given node
// when x has a certain value
double evaluate(double x) {
expressionTreeNode left = getLeftChild();
expressionTreeNode right = getRightChild();
if (left == null && right == null) {
if (value.toString().equals(“x”)) {
return x;
} else {
return new Double(value.toString());
}
}
String op = value.toString();
if (op.equals(“add”)) {
return left.evaluate(x) + right.evaluate(x);
} else if (op.equals(“mult”)) {
return left.evaluate(x) * right.evaluate(x);
} else if (op.equals(“minus”)) {
return left.evaluate(x) – right.evaluate(x);
} else if (op.equals(“cos”)) {
return java.lang.Math.cos(left.evaluate(x));
} else if (op.equals(“sin”)) {
return java.lang.Math.sin(left.evaluate(x));
} else if (op.equals(“exp”)) {
return java.lang.Math.exp(left.evaluate(x));
} else {
System.out.println(“Invalid operator in eval(): ” + op);
}
return 0;
}

/* returns the root of a new expression tree representing the derivative of the
original expression */
expressionTreeNode differentiate() {
expressionTreeNode left = getLeftChild();
expressionTreeNode right = getRightChild();
if (left == null && right == null) {
if (value.toString().equals(“x”)) {
return new expressionTreeNode(“1”);
} else {
return new expressionTreeNode(“0”);
}
}
String op = value.toString();
if (op.equals(“add”)) {
return new expressionTreeNode(“add”, left.differentiate(), right.differentiate(), null);
} else if (op.equals(“mult”)) {
expressionTreeNode prod = new expressionTreeNode(“add”, null, null, null);
prod.setLeftChild(new expressionTreeNode(“mult”, right.differentiate(), left, null));
prod.setRightChild(new expressionTreeNode(“mult”, left.differentiate(), right, null));
return prod;
} else if (op.equals(“minus”)) {
return new expressionTreeNode(“minus”, left.differentiate(), right.differentiate(), null);
} else if (op.equals(“cos”)) {
expressionTreeNode copy = deepCopy();
copy.setValue(“sin”);
expressionTreeNode prod = new expressionTreeNode(“mult”, left.differentiate(), null, null);
prod.setRightChild(new expressionTreeNode(“minus”, new expressionTreeNode(“0”), copy, null));
return prod;
} else if (op.equals(“sin”)) {
expressionTreeNode copy = deepCopy();
copy.setValue(“cos”);
expressionTreeNode prod = new expressionTreeNode(“mult”, left.differentiate(), null, null);
prod.setRightChild(copy);
return prod;
} else if (op.equals(“exp”)) {
expressionTreeNode copy = deepCopy();
expressionTreeNode prod = new expressionTreeNode(“mult”, left.differentiate(), null, null);
prod.setRightChild(copy);
return prod;
} else {
System.out.println(“Invalid operator in diff(): ” + op);
}
return new expressionTreeNode(“0”);
}

public static void main(String args[]) {
expressionTreeNode e = new expressionTreeNode(“mult(add(2,x),cos(x))”);
System.out.println(e);
System.out.println(e.evaluate(1));
System.out.println(e.differentiate());

}
}

OUTPUT

Still stressed from student homework?
Get quality assistance from academic writers!