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.

There is an <strong>in</strong>terest<strong>in</strong>g application of the preorder traversal algorithm that produces<br />

a str<strong>in</strong>g representation of an entire tree. Let us assume aga<strong>in</strong> that for each element e<br />

stored <strong>in</strong> tree T, call<strong>in</strong>g e.toStr<strong>in</strong>g() returns a str<strong>in</strong>g associated with e. The<br />

parenthetic str<strong>in</strong>g representation P(T) of tree T is recursively def<strong>in</strong>ed as follows. If<br />

T consists of a s<strong>in</strong>gle node v, then<br />

P(T) = v.element().toStr<strong>in</strong>g().<br />

Otherwise,<br />

P(T) = v.element().toStr<strong>in</strong>g() + " (" + P(T 1 ) + "," + ··· + ", "+ P(T k ) +")",<br />

where v is the root of T <strong>and</strong> T 1 , T 2 ,..., T k are the subtrees rooted at the children of v,<br />

which are given <strong>in</strong> order if T is an ordered tree.<br />

Note that the above def<strong>in</strong>ition of P(T) is recursive. Also, we are us<strong>in</strong>g "+" here to<br />

denote str<strong>in</strong>g concatenation. The parenthetic representation of the tree of Figure 7.2<br />

is shown <strong>in</strong> Figure 7.7.<br />

Figure 7.7: Parenthetic representation of the tree of<br />

Figure 7.2. Indentation, l<strong>in</strong>e breaks <strong>and</strong> spaces have<br />

been added for clarity.<br />

Note that, technically speak<strong>in</strong>g, there are some computations that occur between<br />

<strong>and</strong> after the recursive calls at a node's children <strong>in</strong> the above algorithm. We still<br />

consider this algorithm to be a preorder traversal, however, s<strong>in</strong>ce the primary action<br />

of pr<strong>in</strong>t<strong>in</strong>g a node's contents occurs prior to the recursive calls.<br />

The <strong>Java</strong> method parentheticRepresentation, shown <strong>in</strong> Code Fragment<br />

7.10, is a variation of method toStr<strong>in</strong>gPreorder (Code Fragment 7.9). It<br />

implements the def<strong>in</strong>ition given above to output a parenthetic str<strong>in</strong>g representation<br />

of a tree T. As with the method toStr<strong>in</strong>gPreorder, the method<br />

parentheticRepresentation makes use of the toStr<strong>in</strong>g method that<br />

is def<strong>in</strong>ed for every <strong>Java</strong> object. In fact, we can view this method as a k<strong>in</strong>d of<br />

toStr<strong>in</strong>g() method for tree objects.<br />

390

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

Saved successfully!

Ooh no, something went wrong!