L16_ExpressionTree

L16_ExpressionTree - Expression Tree An expression tree is...

Info iconThis preview shows pages 1–17. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Expression Tree An expression tree is a tree that represents an expression. = * 3 + x 1 y y = 3 * (x + 1) Page 1 of 43 Expression Tree An expression tree is a tree that represents an expression. + * y * x 2 3 2*x + 3*y Page 2 of 43 Expression Tree An expression tree is a tree that represents an expression. – / * y x y x x – (y * (x / y)) Page 3 of 43 Expression Tree An expression tree is a tree that represents an expression. + 1 – * 2 4 3 1 + (4 * 3 – 2) Page 4 of 43 Expression Tree An expression tree is a tree that represents an expression. – + 2 1 * 3 4 (1 + 4 * 3) – 2 Page 5 of 43 Expression Tree An expression tree is a tree that represents an expression. 5 5 Page 6 of 43 Expression Tree An expression tree is a tree that represents an expression. x x Page 7 of 43 Expression Tree This is NOT an expression tree. – + 2 1 * 4 Page 8 of 43 Expression Tree This is NOT an expression tree. 3 + 2 1 * Page 9 of 43 Evaluating an Expression + * * 8 17 2 x 4 3 y 3 9 {x = 4, y = 3} 2*x + 3*y Page 10 of 43 Evaluating an Expression – + 2 1 * 3 12 13 11 4 1 + 4 * 3 – 2 Page 11 of 43 Evaluating an Expression: Another Viewpoint + 13 * * y 3 x 2 10 3 {x = 8, y = 1} Page 12 of 43 Evaluating an Expression: Another Viewpoint – 11 + 2 13 1 * 3 4 12 1 + 4 * 3 – 2 Page 13 of 43 Evaluating an Expression: Another Viewpoint – + 2 1 * 3 4 12 13 11 1 + 4 * 3 – 2 ExprTree t1 = new …; … int Result = t1.eval(); /* In the implementation class of ExprTree */ public int eval() { … … LHS = this.leftSubtree().eval(); RHS = this.rightSubtree().eval(); op = this.root(); return (LHS op RHS); } Page 14 of 43 Evaluating an Expression: Another Viewpoint Page 15 of 43 5 5 /* In the implementation class of ExprTree */ public int eval() { … if (this.height()==1) return this.root(); LHS = this.leftSubtree().eval(); RHS = this.rightSubtree().eval(); op = this.root(); return (LHS op RHS); } Evaluating an Expression: Another Viewpoint /* In the implementation class of ExprTree */ public int eval() { … if (this.height()==1) return this.root(); LHS = this.leftSubtree().eval(); RHS = this.rightSubtree().eval(); op = this.root(); return (LHS op RHS); } What should this.root() return?...
View Full Document

Page1 / 43

L16_ExpressionTree - Expression Tree An expression tree is...

This preview shows document pages 1 - 17. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online