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.

O(h) time, where h is the height of T. Thus, a b<strong>in</strong>ary search tree T is an efficient<br />

implementation of a dictionary with n entries only if the height of T is small. In<br />

the best case, T has height h = log(n + 1), which yields logarithmic-time<br />

performance for all the dictionary operations. In the worst case, however, T has<br />

height n, <strong>in</strong> which case it would look <strong>and</strong> feel like an ordered list implementation<br />

of a dictionary. Such a worst-case configuration arises, for example, if we <strong>in</strong>sert a<br />

series of entries with keys <strong>in</strong> <strong>in</strong>creas<strong>in</strong>g or decreas<strong>in</strong>g order. (See Figure 10.6.)<br />

Figure 10.6: Example of a b<strong>in</strong>ary search tree with<br />

l<strong>in</strong>ear height, obta<strong>in</strong>ed by <strong>in</strong>sert<strong>in</strong>g entries with keys <strong>in</strong><br />

<strong>in</strong>creas<strong>in</strong>g order.<br />

The performance of a dictionary implemented with a b<strong>in</strong>ary search tree is<br />

summarized <strong>in</strong> the follow<strong>in</strong>g proposition <strong>and</strong> <strong>in</strong> Table 10.1.<br />

Proposition 10.1: A b<strong>in</strong>ary search tree T with height h for n key-value<br />

entries uses O(n) space <strong>and</strong> executes the dictionary ADT operations with the<br />

follow<strong>in</strong>g runn<strong>in</strong>g times. Operationssize <strong>and</strong>isEmpty each take O(1) time.<br />

Operationsf<strong>in</strong>d, <strong>in</strong>sert, <strong>and</strong>remove each take O(h) time. The<br />

operationf<strong>in</strong>dAll takes O(h + s) time, where s is the size of the collection<br />

returned.<br />

Table 10.1: Runn<strong>in</strong>g times of the ma<strong>in</strong> methods of a<br />

dictionary realized by a b<strong>in</strong>ary search tree. We denote<br />

the current height of the tree with h <strong>and</strong> the size of<br />

the collection returned by f<strong>in</strong>dAll with s. The space<br />

usage is O(n), where n is the number of entries stored<br />

<strong>in</strong> the dictionary.<br />

591

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

Saved successfully!

Ooh no, something went wrong!