Due: Tuesday 4/16 10:00am
Submission name: w19_expression
For this, you will need the ExpressionTreeNode
and ExpressionTree
classes, which you can get here: https://github.com/nextcs-dw/dwsource/tree/main/Expression
Reminder: Test Thursday 4/18
Expression Tree Nodes:
Modify the ExpressionTreeNode
class as follows:
- Add a new constructor
ExpressionTreeNode(boolean operation, int x, int y)
- If
operation
istrue
, setvalue
to0
andtype
to a randomint
in the range [1, 4]. - If
operation
isfalse
setvalue
to a random integer in the range [-99, 99] andtype
toVALUE
. - Set the other instance variables as they would be in the other provided constructor.
- If
- Add a
toString()
method which should return aString
containing:- The
value
if the node is a value node. - The correct operation symbol (
+ - * /
) if the node is an operator node.
- The
- Change
display
so that is uses thetoString()
result when printing nodes.
Expression Tree:
Modify makeTree
in the ExpressionTree
class as follows:
- Nodes should have a random chance of being operator or value nodes. Use this to determine the randomness:
operator = random(1) < float(levels)/numLevels
- Leaf nodes (nodes at the last possible level of the tree) should always be value nodes, regardless of the random calculation above.
- Value nodes should not have any child nodes.
- Operator nodes should always have 2 child nodes.
- The provided
makeTree
includes the code for position a node, but does not include any recursive parts (base case or recursive case).