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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

As is the case for general trees, there are many applications of the preorder<br />

traversal for b<strong>in</strong>ary trees.<br />

Postorder Traversal of a B<strong>in</strong>ary Tree<br />

Analogously, the postorder traversal for general trees (Code Fragment 7.11) can<br />

be specialized for b<strong>in</strong>ary trees, as shown <strong>in</strong> Code Fragment 7.23.<br />

Code Fragment 7.23: Algorithm<br />

b<strong>in</strong>aryPostorder for perform<strong>in</strong>g the postorder<br />

traversal of the subtree of a b<strong>in</strong>ary tree T rooted at<br />

node v.<br />

Expression Tree Evaluation<br />

The postorder traversal of a b<strong>in</strong>ary tree can be used to solve the expression tree<br />

evaluation problem. In this problem, we are given an arithmetic expression tree,<br />

that is, a b<strong>in</strong>ary tree where each external node has a value associated with it <strong>and</strong><br />

each <strong>in</strong>ternal node has an arithmetic operation associated with it (see Example<br />

7.9), <strong>and</strong> we want to compute the value of the arithmetic expression represented<br />

by the tree.<br />

Algorithm evaluateExpression, given <strong>in</strong> Code Fragment 7.24, evaluates<br />

the expression associated with the subtree rooted at a node v of an arithmetic<br />

expression tree T by perform<strong>in</strong>g a postorder traversal of T start<strong>in</strong>g at v. In this<br />

419

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

Saved successfully!

Ooh no, something went wrong!