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.

Although the preorder <strong>and</strong> postorder traversals are common ways of visit<strong>in</strong>g the<br />

nodes of a tree, we can also imag<strong>in</strong>e other traversals. For example, we could<br />

traverse a tree so that we visit all the nodes at depth d before we visit the nodes at<br />

depth d + 1. Consecutively number<strong>in</strong>g the nodes of a tree T as we visit them <strong>in</strong><br />

this traversal is called the level number<strong>in</strong>g of the nodes of T (see Section 7.3.5).<br />

7.3 B<strong>in</strong>ary Trees<br />

A b<strong>in</strong>ary tree is an ordered tree with the follow<strong>in</strong>g properties:<br />

1. Every node has at most two children.<br />

2. Each child node is labeled as be<strong>in</strong>g either a left child or a right child.<br />

3. A left child precedes a right child <strong>in</strong> the order<strong>in</strong>g of children of a node.<br />

The subtree rooted at a left or right child of an <strong>in</strong>ternal node v is called a left subtree<br />

or right subtree, respectively, of v. A b<strong>in</strong>ary tree is proper if each node has either<br />

zero or two children. Some people also refer to such trees as be<strong>in</strong>g full b<strong>in</strong>ary trees.<br />

Thus, <strong>in</strong> a proper b<strong>in</strong>ary tree, every <strong>in</strong>ternal node has exactly two children. A b<strong>in</strong>ary<br />

tree that is not proper is improper.<br />

Example 7.8: An important class of b<strong>in</strong>ary trees arises <strong>in</strong> contexts where we wish<br />

to represent a number of different outcomes that can result from answer<strong>in</strong>g a series of<br />

yes-or-no questions. Each <strong>in</strong>ternal node is associated with a question. Start<strong>in</strong>g at the<br />

root, we go to the left or right child of the current node, depend<strong>in</strong>g on whether the<br />

answer to the question is "Yes" or "No." With each decision, we follow an edge from<br />

a parent to a child, eventually trac<strong>in</strong>g a path <strong>in</strong> the tree from the root to an external<br />

node. Such b<strong>in</strong>ary trees are known as decision trees, because each external node v <strong>in</strong><br />

such a tree represents a decision of what to do if the questions associated with v's<br />

ancestors are answered <strong>in</strong> a way that leads to v. A decision tree is a proper b<strong>in</strong>ary<br />

tree. Figure 7.10 illustrates a decision tree that provides recommendations to a<br />

prospective <strong>in</strong>vestor.<br />

Figure 7.10:<br />

advice.<br />

A decision tree provid<strong>in</strong>g <strong>in</strong>vestment<br />

395

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

Saved successfully!

Ooh no, something went wrong!