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.

Creativity<br />

C-7.1<br />

For each node v <strong>in</strong> a tree T, let pre(v) be the rank of v <strong>in</strong> a preorder traversal of<br />

T, let post(v) be the rank of v <strong>in</strong> a postorder traversal of T, let depth (v) be the<br />

depth of v, <strong>and</strong> let desc(v) be the number of descendents of v, not count<strong>in</strong>g v<br />

itself. Derive a formula def<strong>in</strong><strong>in</strong>g post(v) <strong>in</strong> terms of desc(v), depth(v), <strong>and</strong><br />

pre(v), for each node v <strong>in</strong> T.<br />

C-7.2<br />

Let T be a tree whose nodes store str<strong>in</strong>gs. Give an efficient algorithm that<br />

computes <strong>and</strong> pr<strong>in</strong>ts, for every node v of T, the str<strong>in</strong>g stored at v <strong>and</strong> the height<br />

of the subtree rooted at v.<br />

C-7.3<br />

Design algorithms for the follow<strong>in</strong>g operations for a b<strong>in</strong>ary tree T:<br />

•<br />

preorderNext(v): return the node visited after node v <strong>in</strong> a preorder<br />

traversal of T<br />

•<br />

<strong>in</strong>orderNext(v): return the node visited after node v <strong>in</strong> an <strong>in</strong>order<br />

traversal of T<br />

•<br />

postorderNext(v): return the node visited after node v <strong>in</strong> a postorder<br />

traversal of T.<br />

What are the worst-case runn<strong>in</strong>g times of your algorithms?<br />

C-7.4<br />

Give an O(n)-time algorithm for comput<strong>in</strong>g the depth of all the nodes of a tree<br />

T, where n is the number of nodes of T.<br />

C-7.5<br />

The <strong>in</strong>dented parenthetic representation of a tree T is a variation of the<br />

parenthetic representation of T (see Figure 7.7) that uses <strong>in</strong>dentation <strong>and</strong> l<strong>in</strong>e<br />

breaks as illustrated <strong>in</strong> Figure 7.21. Give an algorithm that pr<strong>in</strong>ts this<br />

representation of a tree.<br />

437

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

Saved successfully!

Ooh no, something went wrong!