23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

In Code Fragments 7.31 <strong>and</strong> 7.32, we show classes<br />

EvaluateExpressionTour <strong>and</strong> Pr<strong>in</strong>tExpressionTour, specializ<strong>in</strong>g<br />

EulerTour, that evaluate <strong>and</strong> pr<strong>in</strong>t the arithmetic expression stored <strong>in</strong> a b<strong>in</strong>ary<br />

tree, respectively. Class EvaluateExpressionTour overrides auxiliary<br />

method visitRight(T, v, r) with the follow<strong>in</strong>g computation:<br />

• If v is an external node, set r.out equal to the value of the variable stored<br />

at v<br />

• Else (v is an <strong>in</strong>ternal node), comb<strong>in</strong>e r.left <strong>and</strong> r.right with the<br />

operator stored at v, <strong>and</strong> set r.out equal to the result of the operation.<br />

Class Pr<strong>in</strong>tExpressionTour overrides methods visitLeft,<br />

visitBelow, <strong>and</strong> visitRight follow<strong>in</strong>g the approach of pseudo-code<br />

version shown <strong>in</strong> Code Fragment 7.27.<br />

430

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!