9.4.3 Expression Trees Another application of trees is to store mathematical expressions such as 15*(x+y) or sqrt(42)+7 in a convenient form. Let's stick for the moment to expressions made up of numbers and the operators +, -, *, and /. Consider the expression 3*((7+1)/4)+(17-5). This expression is made up of two subexpressions, 3*((7+1)/4) and (17-5), combined with the operator "+". When the expression is represented as a binary tree, the root node holds the operator +, while the subtrees of the root node represent the subexpressions 3*((7+1)/4) and (17-5). Every node in the tree holds either a number or an operator. A node that holds a number is a leaf node of the tree. A node that holds an operator has two subtrees representing the operands to which the operator applies. The tree is shown in the illustration below. I will refer to a tree of this type as an expression tree. Given an expression tree, it's easy to find the value of the expression that it represents. Each node in the tree has an associated value. If the node is a leaf node, then its value is simply the number that the node contains. If the node contains an operator, then the associated value is computed by first finding the values of its child nodes and then applying the operator to those values. The process is shown by the upward-directed arrows in the illustration. The value computed for the root node is the value of the expression as a whole. There are other uses for expression trees. For example, a postorder traversal of the tree will output the postfix form of the expression. An expression tree contains two types of nodes: nodes that contain numbers and nodes that contain operators. Furthermore, we might want to add other types of nodes to make the trees more useful, such as nodes that contain variables. If we want to work with expression trees in Java, how can we deal with this variety of nodes? One way -- which will be frowned upon by object-oriented purists -- is to include an instance variable in each node object to record which type of node it is: enum NodeType { NUMBER, OPERATOR } // Possible kinds of node. class ExpNode { // A node in an expression tree. NoteType kind; // Which type of node is this? double number; // The value in a node of type NUMBER. char op; // The operator in a node of type OPERATOR. ExpNode left; // Pointers to subtrees, ExpNode right; // in a node of type OPERATOR. ExpNode( double val ) { // Constructor for making a node of type NUMBER. kind = NodeType.NUMBER; number = val; } ExpNode( char op, ExpNode left, ExpNode right ) { // Constructor for making a node of type OPERATOR. kind = NodeType.OPERATOR; this.op = op; this.left = left; this.right = right; } } // end class ExpNode Given this definition, the following recursive subroutine will find the value of an expression tree: static double getValue( ExpNode node ) { // Return the value of the expression represented by // the tree to which node refers. Node must be non-null. if ( node.kind == NodeType.NUMBER ) { // The value of a NUMBER node is the number it holds. return node.number; } else { // The kind must be OPERATOR. // Get the values of the operands and combine them // using the operator. double leftVal = getValue( node.left ); double rightVal = getValue( node.right ); switch ( node.op ) { case '+': return leftVal + rightVal; case '-': return leftVal - rightVal; case '*': return leftVal * rightVal; case '/': return leftVal / rightVal; default: return Double.NaN; // Bad operator. } } } // end getValue() Although this approach works, a more object-oriented approach is to note that since there are two types of nodes, there should be two classes to represent them, ConstNode and BinOpNode. To represent the general idea of a node in an expression tree, we need another class, ExpNode. Both ConstNode and BinOpNode will be subclasses of ExpNode. Since any actual node will be either a ConstNode or a BinOpNode, ExpNode should be an abstract class. (See Subsection 5.5.5.) Since one of the things we want to do with nodes is find their values, each class should have an instance method for finding the value: abstract class ExpNode { // Represents a node of any type in an expression tree. abstract double value(); // Return the value of this node. } // end class ExpNode class ConstNode extends ExpNode { // Represents a node that holds a number. double number; // The number in the node. ConstNode( double val ) { // Constructor. Create a node to hold val. number = val; } double value() { // The value is just the number that the node holds. return number; } } // end class ConstNode class BinOpNode extends ExpNode { // Represents a node that holds an operator. char op; // The operator. ExpNode left; // The left operand. ExpNode right; // The right operand. BinOpNode( char op, ExpNode left, ExpNode right ) { // Constructor. Create a node to hold the given data. this.op = op; this.left = left; this.right = right; } double value() { // To get the value, compute the value of the left and // right operands, and combine them with the operator. double leftVal = left.value(); double rightVal = right.value(); switch ( op ) { case '+': return leftVal + rightVal; case '-': return leftVal - rightVal; case '*': return leftVal * rightVal; case '/': return leftVal / rightVal; default: return Double.NaN; // Bad operator. } } } // end class BinOpNode Note that the left and right operands of a BinOpNode are of type ExpNode, not BinOpNode. This allows the operand to be either a ConstNode or another BinOpNode -- or any other type of ExpNode that we might eventually create. Since every ExpNode has a value() method, we can call left.value() to compute the value of the left operand. If left is in fact a ConstNode, this will call the value() method in the ConstNode class. If it is in fact a BinOpNode, then left.value() will call the value() method in the BinOpNode class. Each node knows how to compute its own value. Although it might seem more complicated at first, the object-oriented approach has some advantages. For one thing, it doesn't waste memory. In the original ExpNode class, only some of the instance variables in each node were actually used, and we needed an extra instance variable to keep track of the type of node. More important, though, is the fact that new types of nodes can be added more cleanly, since it can be done by creating a new subclass of ExpNode rather than by modifying an existing class. We'll return to the topic of expression trees in the next section, where we'll see how to create an expression tree to represent a given expression.