In this assignment you will write a program that will take a mathematical expression written in post-fix notation and generate an expression tree from it. Normally, we use what’s called in-fix notation like: 1 + 1. Post-fix puts the operator after the two numbers like: 1 1 +. An expression tree is a type of binary tree that has the operators as parents of the two values that go into them.
Your program must do the following in order to receive full credit on this assignment.
a. In order for this method to return true, every element must be either a number or
an operator. If any of them are not, it should return false.
a. Since a NumberNode does not have any children, what value should getLeft and getRight return?
7. Create a new class called OperatorNode, which represents the operators stored in the tree with two Nodes under it. This should also implement the Node interface.
i. What type should these be?
8. As with NumberNode, implement the methods from the Node interface.
a. The tricky one this time is getValue. We don’t have an int value to return. What do we do when when a class inherits or implements a method that it does not support?
9. As always when dealing with user input, we have no idea what they will give us. They might give us garbage, so we will need some error handling. While there are other potential ways to handle these, we will be using exceptions. To start, create a new class called OperatorOnlyException. This exception will be thrown when the user provides an expression that is only operators with no numbers in it.
a. b.
12. In the a. b. c.
This also must inherit from RuntimeException.
Again, create a single default constructor that generates a message that the expression is invalid.
ExpressionTree class implement a new method called parseExpression.
It is public and does not return a value.
It takes an array of strings as a parameter.
It can throw either of the exceptions we’ve defined, but it will also possibly throw an EmptyCollectionException as we’ll soon see, so be sure to add a throws clause for all three of these.
13. Before we can try to parse the expression, we must first check if we even can. There are several possible situations we need to check for.
a. If the entire expression is operators, throw an OperatorOnlyException.
i. The rest of these situations will throw an InvalidExpressionException.
i. Might sound odd but think about it. d. If there are any invalid terms.
i. Keep in mind, the left child of one of these pairs would be the OperatorNode from the previous set.
c. You should assume that this is the pattern you are given. There is a scenario that passes the above checks, but should cause you to pop from an empty stack. This is what causes the EmptyStackCollection exception to be thrown. Think about what
that scenario would be as it might help you figure out how to use the stack if
you’re not sure yet.
d. At the end make sure the top Node is set as the root.
16. Implement a way to do an in-order traversal of the expression tree to generate the in-fix notation version of the expression.
a. You have two basic choices for doing this. You can write new method in ExpressionTree that will do it (recursion will help) or you can implement the provided toString method stub (This will instead have you implement toString methods in your Node classes). The choice is yours.
17. In the Assignment12 class, complete the main method to try and parse the expression.
Example Inputs
Hi there! Click one of our representatives below and we will get back to you as soon as possible.