Created
          November 4, 2016 01:27 
        
      - 
      
- 
        Save jjlumagbas/b31ef36a713102fceba89468b7be6ded to your computer and use it in GitHub Desktop. 
    Evaluate expression tree
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | public class ExpressionTree { | |
| public static int eval(MyTreeNode root) { | |
| return 0; | |
| } | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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)); | |
| } | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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