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.

Code Fragment 7.10: Algorithm<br />

parentheticRepresentation. Note the use of the +<br />

operator to concatenate two str<strong>in</strong>gs.<br />

We explore a modification to Code Fragment 7.10 <strong>in</strong> Exercise R-7.9, to display a<br />

tree <strong>in</strong> a fashion more closely match<strong>in</strong>g that given <strong>in</strong> Figure 7.7.<br />

7.2.3 Postorder Traversal<br />

Another important tree traversal algorithm is the postorder traversal. This<br />

algorithm can be viewed as the opposite of the preorder traversal, because it<br />

recursively traverses the subtrees rooted at the children of the root first, <strong>and</strong> then<br />

visits the root. It is similar to the preorder traversal, however, <strong>in</strong> that we use it to<br />

solve a particular problem by specializ<strong>in</strong>g an action associated with the "visit" of a<br />

node v. Still, as with the preorder traversal, if the tree is ordered, we make recursive<br />

calls for the children of a node v accord<strong>in</strong>g to their specified order. Pseudo-code for<br />

the postorder traversal is given <strong>in</strong> Code Fragment 7.11.<br />

Code Fragment 7.11: Algorithm postorder for<br />

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

tree T rooted at a node v.<br />

391

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

Saved successfully!

Ooh no, something went wrong!