Class ExpressionVisualizer

java.lang.Object
  |
  +--ExpressionVisualizer

public class ExpressionVisualizer
extends Object

Use this class to get String representations of mathematical expressions constructed of Node objects.

Infix expression format

An expression in Infix is formatted as follows: (operand1 operator1 operand2). Expressions in parentheses can be nested, for example ((operand1 operator1 operand2) operator2 operand3). Parentheses are required to determine the order of operations.

Postfix expression format

An expression in Postfix is formatted as follows: operand1 operand2 operator1. Expressions can be nested, for example operand1 operand2 operator1 operand3 operator2. Parentheses are not required because the number of operands an operator takes is known (for example, a BinaryOperator takes exactly two parameters).

Prefix expression format

An expression in Prefix is formatted as follows: operator1 operand1 operand2. Expressions can be nested, for example operator1 operator2 operand1 operand2 operand3 The number of operands that an operator takes is known, so parentheses are not required.

Elisp expression format

An expression in Elisp is formatted as follows: (operator1 operand1 operand2). Expressions can be nested, for example (operator1 (operator2 operand1 operand2) operand3). Parentheses are required to determine an operator from an operand.

All examples used in method descriptions came form the same expression tree.


Constructor Summary
ExpressionVisualizer(Node root)
          Constructs an ExpressionVisualizer.
 
Method Summary
 String elispFix()
          Returns the expression in a form fit for Elisp evaluation.
 String inFix()
          Returns the infix representation of the expression.
 String postFix()
          Returns the postfix representation of the expression.
 String preFix()
          Returns the prefix representation of the expression.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ExpressionVisualizer

public ExpressionVisualizer(Node root)
Constructs an ExpressionVisualizer.
Parameters:
root - The root of the expression tree you wish to see.
Method Detail

inFix

public String inFix()
Returns the infix representation of the expression. The infix technique is probably the most familiar to you. It is called "infix" because the operators are "in"-between the operands. This method requires parentheses to determine the order of operations. For example, (((7*3)-(10/2))+(5/(-3+4)))

postFix

public String postFix()
Returns the postfix representation of the expression. The postfix technique is used by stack-based calculators such as the HP48G. It is called "postfix" because the operators are after the operands. This method requires no parentheses. For example, 7 3 * 10 2 / - 5 -3 4 + / +

preFix

public String preFix()
Returns the prefix representation of the expression. The prefix technique is used by some compilers. It is called "prefix" because the operator comes before its operands. This makes sense for a compiler because oftentimes the operands are variables and they need to be fetched from memory, while the operator is already known. This method requires no parentheses. For example: + - * 7 3 / 10 2 / 5 + -3 4

elispFix

public String elispFix()
Returns the expression in a form fit for Elisp evaluation. Emacs can be programmed with a language called Elisp, a derivative of the Lisp language. Elisp uses a type of prefix notation, but with parentheses. For example: (+ (- (* 7 3) (/ 10 2)) (/ 5 (+ -3 4)))