Browse Source

Finished evaluate recursively

master
Raphael Roberts 6 years ago
parent
commit
a428c47c2d
  1. 19
      BST.java

19
BST.java

@ -91,6 +91,23 @@ public class BST {
// This method takes a reference to the root of the expression, evaluates
// the tree, and returns the result as an int.
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());
}
}
}
Loading…
Cancel
Save