Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.31 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3.  
  4. public class ExpressionTree {
  5.  
  6.     private static class Node{
  7.        
  8.         private Node left;
  9.         private Node right;
  10.         private Object item;
  11.         private boolean isLeaf;
  12.        
  13.         private Node(boolean isLeaf, Object item)
  14.         {
  15.             this.item = item;
  16.             this.isLeaf = isLeaf;
  17.             this.left = null;   //Start empty
  18.             this.right = null;         
  19.         }
  20.     }
  21.    
  22.     Node root = null;
  23.    
  24.     public ExpressionTree (String input)
  25.     {  root = build(input);  }
  26.  
  27.    
  28.     private Node build(Scanner input) {
  29.           boolean  leaf;
  30.           String   token;
  31.           double   value;
  32.           Node node;
  33.  
  34.           leaf = input.hasNextDouble();
  35.           if ( leaf )
  36.           {
  37.              value = input.nextDouble();
  38.              node = new Node ( leaf, value );
  39.           }
  40.           else
  41.           {
  42.              token = input.next();
  43.              node  = new Node ( leaf, token.charAt(0));
  44.              node.left  = build ( input );
  45.              node.right = build ( input );
  46.           }
  47.           return node;
  48.     }
  49.    
  50.     public void insert(Object item) {
  51.         if(root == null)
  52.             root = new Node(item);
  53.        
  54.         Node runner = root;
  55.        
  56.         if (value < node.value) {
  57.             if (node.left != null) {
  58.                 insert(node.left, value);
  59.             } else {
  60.                 System.out.println("  Inserted " + value +
  61.                                 " to left of node " + node.value);
  62.                 node.left = new Node(value);
  63.             }
  64.         } else if (value > node.value) {
  65.             if (node.right != null) {
  66.                 insert(node.right, value);
  67.             } else {
  68.                 System.out.println("  Inserted " + value + "
  69.                to right of node " + node.value);
  70.                 node.right = new Node(value);
  71.             }
  72.         }
  73.     }
  74.    
  75.       public double evaluate ()
  76.        {  return root == null ? 0.0 : evaluate ( root ) ;  }
  77.  
  78.        // Evaluate the expression:  for internal nodes, this amounts
  79.        // to a post-order traversal, in which the processing is doing
  80.        // the actual arithmetic.  For leaf nodes, it is simply the
  81.        // value of the node.
  82.        private double evaluate ( Node node )
  83.        {
  84.           double result;    // Value to be returned
  85.  
  86.           if ( node.isLeaf )        // Just get the value of the leaf
  87.              result = Double.valueOf(node.item.toString());
  88.           else
  89.           {
  90.              // We've got work to do, evaluating the expression
  91.              double left, right;
  92.              char   operator = node.item.toString().charAt(0);
  93.  
  94.              // Capture the values of the left and right subexpressions
  95.              left  = evaluate ( node.left );
  96.              right = evaluate ( node.right );
  97.  
  98.              // Do the arithmetic, based on the operator
  99.              switch ( operator )
  100.              {
  101.                 case '-':  result = left - right;  break;
  102.                 case '*':  result = left * right;  break;
  103.                 case '/':  result = left / right;  break;
  104.                 case '^':  result = Math.pow (left, right );  break;
  105.              // NOTE:  allow fall-through from default to case '+'
  106.                 default:   System.out.println ("Unrecognized operator " +
  107.                               operator + " treated as +.");
  108.                 case '+':  result = left + right;  break;
  109.              }
  110.           }
  111.           // Return either the leaf's value or the one we just calculated.
  112.           return result;
  113.        }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement