A compiler uses a binary tree to represent an arithmetic expression. The nodes of the tree, called an expression tree, are binary operators and operands. We will now develop an algorithm that shows you how to take an arithmetic expression in postfix notation and create the expression tree. You can refer back to Chapter 14 in which we introduced infix and postfix notation for an arithmetic expression. These formats specify the position of a binary operator and its operands. In postfix notation, the binary operator comes after its operands, and in infix notation, the operator appears between its operands. A third notation, called prefix notation, places a binary operator before its operands. The expressions in below table include each of the formats :
Assume an arithmetic expression involves the binary operators addition (+), subtraction (-), multiplication (*) and division (/). In the expression tree, each operator has two children that are either operands or subexpressions. A binary expression tree consists of :
The trees in below figure describe the expression in upper table :
The preorder and postorder traversals of a binary expression tree produce the prefix and postfix notation for the expression. An inorder traversal generates the infix form of the expression, assuming that parentheses are not needed to determine the order of evaluation. For instance, in upper figure (c), the following are different traversals of the expression : a + b * c / d - e
Building a Binary Expression Tree :
To build an expression tree, we need to develop an iterative algorithm that takes a string containing an expression in postfix form. An operand is a single character such as 'a' or 'b'. Our algorithm follows the steps of the postfix evaluation algorithm in Section 14.4. A stack holds the operands, which in this case are trees. More specifically, the stack holds TNode references that are the root of subtree operands. In the postfix evaluation algorithm, whenever we located an operator, we popped its two operands from the stack and computed the result. In this algorithm, when we locate an operator, we construct a subtree and insert its root reference onto the stack. The following is a description of the action taken when we encounter an operand or an operator in the input string :
The method buildExpTree() implements the algorithm. The following steps illustrate the action of the method for the expression a+b*c in postfix form. We represent the stack horizontally to simply the view of the elements. Remember, each element is a subtree specified by its root :
Considering postfix expression : a b c * +
Step1: Recognize 'a' as an operand. Construct a leaf node containing the Character value 'a' and push its reference onto the stack s :
Step2-3: Recognize 'b' and 'c' as operands, construct leaf nodes, and push their references onto the stack.
Step 4: Recognize '*' as an operator. Create a new node with '*' as its value. Then pop two subtrees (node 'c' and node 'b') from the stack. These are the right and left node respectively. Attach the subtrees and push the new subtree (root '*') on the stack.
Step 5: Recognize '+' as an operator. Create a new node with '+' as its value. Pop its two operands from the stack, attach them to the node, and push the new subtree (root '+') on the stack.
Step 6: We have reached the end of the expression. The single item on the stack is the root of the expression tree.
The method buildExpTree() applies this algorithm to construct the expression tree for a correctly formed postfix expression. The method performs no error checking. It assumes that an operand is a single character and that the available operators are +, - , * and /. In addition, the expression can contain the whitespace characters blank or tab. Below is the implementation :
Actually, it will build a binary tree as below :
沒有留言:
張貼留言