CS 119 Lab 6 - Trees
Objectives
1. We wish to parse an arithmetic infix expression. This means that we are translating the expression into a tree representing the computation. The infix expression is represented as a list. For example ‘(4 + 3 * 7 - 14 / ( 3 + 4) + 6).
Each element of the list must be one of three things: a number; one of the four symbols +, -, *, or /; or a sublist such as (3 + 4) in this example.
What makes this problem tricky is that we can’t put the list elements into an expression tree as soon as we see them, but must instead consider the precedence of the operators. As we examine the expression from left to right, we must maintain information about the elements that have been examined but not entirely processed. We will do this in two lists: one list for still pending operations and another for still pending operands. The operand list will contain subtrees ready to be combined later on.
Here is the algorithm:
|
Assignment: |
2. We now can use the parse tree to perform the computation. This should be quite simple. If the entry in the tree is a number then that is the value of the computation. Otherwise, if the entry is an operator, recursively compute the value of the two subtrees and then perform the appropriate operation on those values.
|
Assignment: |
3. Email your files containing the assignments to jzimmerm@goucher.edu.