From a428c47c2d0f21924d176bca3668765e64bae435 Mon Sep 17 00:00:00 2001 From: Raphael Roberts Date: Mon, 11 Nov 2019 12:48:10 -0600 Subject: [PATCH] Finished evaluate recursively --- BST.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/BST.java b/BST.java index a064fbe..24752f0 100644 --- a/BST.java +++ b/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()); + } + } }