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.

case, the "visit" action consists of perform<strong>in</strong>g a s<strong>in</strong>gle arithmetic operation. Note<br />

that we use the fact that an arithmetic expression tree is a proper b<strong>in</strong>ary tree.<br />

Code Fragment 7.24: Algorithm<br />

evaluateExpression for evaluat<strong>in</strong>g the expression<br />

represented by the subtree of an arithmetic expression<br />

tree T rooted at node v.<br />

The expression-tree evaluation application of the postorder traversal provides an<br />

O(n)-time algorithm for evaluat<strong>in</strong>g an arithmetic expression represented by a<br />

b<strong>in</strong>ary tree with n nodes. Indeed, like the general postorder traversal, the<br />

postorder traversal for b<strong>in</strong>ary trees can be applied to other "bottom-up" evaluation<br />

problems (such as the size computation given <strong>in</strong> Example 7.7) as well.<br />

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

An additional traversal method for a b<strong>in</strong>ary tree is the <strong>in</strong>order traversal. In this<br />

traversal, we visit a node between the recursive traversals of its left <strong>and</strong> right<br />

subtrees. The <strong>in</strong>order traversal of the subtree rooted at a node v <strong>in</strong> a b<strong>in</strong>ary tree T<br />

is given <strong>in</strong> Code Fragment 7.25.<br />

Code Fragment 7.25: Algorithm <strong>in</strong>order for<br />

perform<strong>in</strong>g the <strong>in</strong>order traversal of the subtree of a<br />

b<strong>in</strong>ary tree T rooted at a node v.<br />

420

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

Saved successfully!

Ooh no, something went wrong!