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.

Analysis of B<strong>in</strong>ary Tree Search<strong>in</strong>g<br />

The analysis of the worst-case runn<strong>in</strong>g time of search<strong>in</strong>g <strong>in</strong> a b<strong>in</strong>ary search tree T<br />

is simple. Algorithm TreeSearch is recursive <strong>and</strong> executes a constant number<br />

of primitive operations for each recursive call. Each recursive call of<br />

TreeSearch is made on a child of the previous node. That is, TreeSearch is<br />

called on the nodes of a path of T that starts at the root <strong>and</strong> goes down one level at<br />

a time. Thus, the number of such nodes is bounded by h + 1, where h is the height<br />

of T. In other words, s<strong>in</strong>ce we spend O(1) time per node encountered <strong>in</strong> the<br />

search, method f<strong>in</strong>d on dictionary D runs <strong>in</strong> O(h) time, where h is the height of<br />

the b<strong>in</strong>ary search tree T used to implement D. (See Figure 10.2.)<br />

Figure 10.2: Illustrat<strong>in</strong>g the runn<strong>in</strong>g time of<br />

search<strong>in</strong>g <strong>in</strong> a b<strong>in</strong>ary search tree. The figure uses<br />

st<strong>and</strong>ard visualization shortcuts of view<strong>in</strong>g a b<strong>in</strong>ary<br />

search tree as a big triangle <strong>and</strong> a path from the root<br />

as a zig-zag l<strong>in</strong>e.<br />

586

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

Saved successfully!

Ooh no, something went wrong!