Skip to content

Instantly share code, notes, and snippets.

@jjlumagbas
Created November 4, 2016 01:27
Show Gist options
  • Save jjlumagbas/b31ef36a713102fceba89468b7be6ded to your computer and use it in GitHub Desktop.
Save jjlumagbas/b31ef36a713102fceba89468b7be6ded to your computer and use it in GitHub Desktop.
Evaluate expression tree
public class ExpressionTree {
public static int eval(MyTreeNode root) {
return 0;
}
}
import static org.junit.Assert.*;
public class ExpressionTreeTest {
@org.junit.Test
public void eval() throws Exception {
MyTreeNode five = new MyTreeNode("5", null, null);
MyTreeNode four = new MyTreeNode("4", null, null);
MyTreeNode two = new MyTreeNode("2", null, null);
MyTreeNode three = new MyTreeNode("3", null, null);
MyTreeNode times1 = new MyTreeNode("*", five, four);
MyTreeNode times2 = new MyTreeNode("*", two, three);
MyTreeNode plus = new MyTreeNode("+", times1, times2);
assertEquals(26, ExpressionTree.eval(plus));
}
}
public class MyTreeNode {
private String data;
private MyTreeNode left;
private MyTreeNode right;
public MyTreeNode(String data, MyTreeNode left, MyTreeNode right) {
this.data = data;
this.left = left;
this.right = right;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public MyTreeNode getLeft() {
return left;
}
public void setLeft(MyTreeNode left) {
this.left = left;
}
public MyTreeNode getRight() {
return right;
}
public void setRight(MyTreeNode right) {
this.right = right;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment