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.

Edges <strong>and</strong> Paths <strong>in</strong> Trees<br />

An edge of tree T is a pair of nodes (u, v) such that u is the parent of v, or vice<br />

versa. A path of T is a sequence of nodes such that any two consecutive nodes <strong>in</strong><br />

the sequence form an edge. For example, the tree <strong>in</strong> Figure 7.3 conta<strong>in</strong>s the path<br />

(cs252/, projects/, demos/, market).<br />

Example 7.2: The <strong>in</strong>heritance relation between classes <strong>in</strong> a <strong>Java</strong> program<br />

forms a tree. The root, java. lang. Object, is an ancestor of all other<br />

classes. Each class, C, is a descendent of this root <strong>and</strong> is the root of a subtree of<br />

the classes that extend C. Thus, there is a path from C to the root,<br />

java.lang.Object, <strong>in</strong> this <strong>in</strong>heritance tree.<br />

Ordered Trees<br />

A tree is ordered if there is a l<strong>in</strong>ear order<strong>in</strong>g def<strong>in</strong>ed for the children of each node;<br />

that is, we can identify the children of a node as be<strong>in</strong>g the first, second, third, <strong>and</strong><br />

so on. Such an order<strong>in</strong>g is usually visualized by arrang<strong>in</strong>g sibl<strong>in</strong>gs left to right,<br />

accord<strong>in</strong>g to their order<strong>in</strong>g. Ordered trees typically <strong>in</strong>dicate the l<strong>in</strong>ear order<br />

among sibl<strong>in</strong>gs by list<strong>in</strong>g them <strong>in</strong> the correct order.<br />

Example 7.3: The components of a structured document, such as a book, are<br />

hierarchically organized as a tree whose <strong>in</strong>ternal nodes are parts, chapters, <strong>and</strong><br />

sections, <strong>and</strong> whose external nodes are paragraphs, tables, figures, <strong>and</strong> so on.<br />

(See Figure 7.4.) The root of the tree corresponds to the book itself. We could, <strong>in</strong><br />

fact, consider exp<strong>and</strong><strong>in</strong>g the tree further to show paragraphs consist<strong>in</strong>g of<br />

sentences, sentences consist<strong>in</strong>g of words, <strong>and</strong> words consist<strong>in</strong>g of characters.<br />

Such a tree is an example of an ordered tree, because there is a well-def<strong>in</strong>ed<br />

order<strong>in</strong>g among the children of each node.<br />

Figure 7.4: An ordered tree associated with a book.<br />

379

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

Saved successfully!

Ooh no, something went wrong!