Answered step by step
Verified Expert Solution
Question
1 Approved Answer
An arithmetic expression can be represented as a binary tree where every leaf contains a number and every internal node an arithmetic operator. We call
An arithmetic expression can be represented as a binary tree where every leaf contains a
number and every internal node an arithmetic operator. We call these trees syntax trees.
Here we consider arithmetic expressions comprised of positive integers and the operators
addition and subtraction
For example, the arithmetic expression is represented as the following
syntax tree of height
Note that parentheses are not necessary when arithmetic expressions are in syntax tree
form.
Addition is symmetric and associative thatisabbaand abcabc
Subtraction is neither symmetric nor associative. Therefore, the above arithmetic
expression is equivalent to the expression which is represented as the
following syntax tree of height
We want to take advantage of symmetry and associativity of addition to transform syntax
trees to equivalent ones of minimum height. This transformation should not evaluate the
expressions and therefore the output trees should have the same number of internal nodes
operators and leaves numbers as the input trees.
XSCH
a Describe a set of tree transformations that, when applied to a syntax tree, reduce its
height. This set should be complete: we can apply them repeatedly to a syntax tree to
derive an equivalent tree of minimum height. Explain the transformations and why they are
complete.
marks
b Implement the following method in Java that inputs a syntax tree and outputs an
equivalent one with minimum height. The method should run in time ON where N is the
number of nodes in the input syntax tree.
marks
AExpr compact AExpr inTree
BExpr is the class shown below. The static factory methods makePlusNode,
makeMinusNode, and makeNumberNode create new tree nodes.
class AExpr
private int height;
private boolean isOperator; true: internal node;
false: leaf
when internal node
private char operator; can be or ~
private AZxpr leftOperand;
private AExpr rightOperand;
when leaf
int numbex;
private AExprboolean isOp, char op AExpr left, AExpr
right,
int num, int h
isOperator isOp; operator op; leftOperand left;
rightOperand right; number num; height hi
factory methods to create Syntax Tree nodes
static AExpr makePlusNode AExpr left, AExpr right
return new AExprtrue left, right, O
Math.max leftgetHeight right.getHeight;
static AExpr makeMinusNode AExpr left, AExpr right
return new AExprtrue~ left, right, O
Math.maxleftgetHeight right.getHeight;
static AExpr makeNumberNode int num
XSCH
return new AExprfalse null, null, num, ;
interface with node
boolean isOperatox return isOperator;
boolean isPlus return isOperator && operator ;
boolean isMinus return isOperator && operator ;
boolean isNumber return :isOperator;
RExpr getLeft return leftoperand;
AExpr getRight return rightOperand;
int getNumber return number;
int getHeight return height;
void setLeft AExpr left leftOperand left;
void setRight AExpr right rightOperand right;
void setNumberint num number num;
void setHeight int h height h;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started