It is possible to represent arithmetic, parenthesized expressions using a tree. If a node is an operator, such as a plus or a division sign, then each of the two children must be either a number or an expression which will evaluate to a number. In other words, the two children of an operator will be its operands. The above represents (3 + 4).
Problem : Convert the following expression into such a tree: ((3 + 4)*5)/6The basic procedure is to determine which operations can be done first (that is, those that are not dependent on any other operations). Make trees for those, and then continue this process using the newly formed trees as operands.
Problem : Convert the following expression into such a tree: 3 + 4*(5/6)
Problem : How could you use this tree representation to devise a scheme to represent the expressions without using any parentheses. Hint: Consider a the different sorts of traversals. See the recursion SparkNote for information on tree traversals.If you a post-order traversal, for example, you can create an expression which is unambiguous and does not use parentheses. In math, this form is called postfix notation. The way that it can be unambiguously resolved is that whenever you hit an operator, the two operands for it will be immediately preceding it. For example:
2 3 4 + *
means add the 3 and the 4 and then multiply by 2. Its parenthesized equivalent is: 2*(3 + 4)
Take a Study Break
Every Shakespeare Play Summed Up in a Quote from The Office
Honest Names for All the Books on Your English Syllabus
Pick 5 Books and We'll Tell You What Netflix Show You Should Binge-Watch This Summer
QUIZ: Can You Identify the Shakespeare Play By Its Most Popular Quote?
Every Marvel Movie Summed Up in a Single Sentence
QUIZ: Are You a Hero, a Villain, or an Anti-Hero?
Pick 10 Books and We'll Guess Whether You're an Introvert or an Extrovert