|
|
@ -91,6 +91,23 @@ public class BST { |
|
|
// This method takes a reference to the root of the expression, evaluates |
|
|
// This method takes a reference to the root of the expression, evaluates |
|
|
// the tree, and returns the result as an int. |
|
|
// the tree, and returns the result as an int. |
|
|
public int evaluate(BSTNode node) { |
|
|
public int evaluate(BSTNode node) { |
|
|
return -1; // replace this statement with your own return |
|
|
|
|
|
|
|
|
String value = node.getInfo(); |
|
|
|
|
|
switch (value) { |
|
|
|
|
|
case "+": |
|
|
|
|
|
return evaluate(node.getLeft()) + evaluate(node.getRight()); |
|
|
|
|
|
|
|
|
|
|
|
case "-": |
|
|
|
|
|
return evaluate(node.getLeft()) - evaluate(node.getRight()); |
|
|
|
|
|
|
|
|
|
|
|
case "*": |
|
|
|
|
|
return evaluate(node.getLeft()) * evaluate(node.getRight()); |
|
|
|
|
|
|
|
|
|
|
|
case "/": |
|
|
|
|
|
return evaluate(node.getLeft()) / evaluate(node.getRight()); |
|
|
|
|
|
|
|
|
|
|
|
default: |
|
|
|
|
|
return Integer.parseInt(node.getInfo()); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |