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.

Figure 7.18: A b<strong>in</strong>ary search tree stor<strong>in</strong>g <strong>in</strong>tegers.<br />

The blue solid path is traversed when search<strong>in</strong>g<br />

(successfully) for 36. The blue dashed path is traversed<br />

when search<strong>in</strong>g (unsuccessfully) for 70.<br />

We can use a b<strong>in</strong>ary search tree T for set S to f<strong>in</strong>d whether a given search value y<br />

is <strong>in</strong> S, by travers<strong>in</strong>g a path down the tree T, start<strong>in</strong>g at the root. (See Figure 7.18.)<br />

At each <strong>in</strong>ternal node v encountered, we compare our search value y with the<br />

element x(v) stored at v. If y = x(v), then the search cont<strong>in</strong>ues <strong>in</strong> the left subtree of<br />

v. If y = x(v), then the search term<strong>in</strong>ates successfully. If y ≥ x(v), then the search<br />

cont<strong>in</strong>ues <strong>in</strong> the right subtree of v. F<strong>in</strong>ally, if we reach an external node, the<br />

search term<strong>in</strong>ates unsuccessfully. In other words, a b<strong>in</strong>ary search tree can be<br />

viewed as a b<strong>in</strong>ary decision tree (recall Example 7.8), where the question asked at<br />

each <strong>in</strong>ternal node is whether the element at that node is less than, equal to, or<br />

larger than the element be<strong>in</strong>g searched for. Indeed, it is exactly this<br />

correspondence to a b<strong>in</strong>ary decision tree that motivates restrict<strong>in</strong>g b<strong>in</strong>ary search<br />

trees to be proper b<strong>in</strong>ary trees (with "place-holder" external nodes).<br />

Note that the runn<strong>in</strong>g time of search<strong>in</strong>g <strong>in</strong> a b<strong>in</strong>ary search tree T is proportional to<br />

the height of T. Recall from Proposition 7.10 that the height of a proper b<strong>in</strong>ary<br />

tree with n nodes can be as small as log(n + 1) − 1 or as large as (n − 1)/2. Thus,<br />

b<strong>in</strong>ary search trees are most efficient when they have small height. We illustrate<br />

an example search operation <strong>in</strong> a b<strong>in</strong>ary search tree <strong>in</strong> Figure 7.18, <strong>and</strong> we study<br />

b<strong>in</strong>ary search trees <strong>in</strong> more detail <strong>in</strong> Section 10.1.<br />

Us<strong>in</strong>g Inorder Traversal for Tree Draw<strong>in</strong>g<br />

422

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

Saved successfully!

Ooh no, something went wrong!