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.

Method<br />

Time<br />

size,isEmpty<br />

O(1)<br />

f<strong>in</strong>d, <strong>in</strong>sert, remove<br />

O(h)<br />

O(h + s)<br />

f<strong>in</strong>dAll<br />

Note that the runn<strong>in</strong>g time of search <strong>and</strong> update operations <strong>in</strong> a b<strong>in</strong>ary search tree<br />

varies dramatically depend<strong>in</strong>g on the tree's height. We can nevertheless take<br />

comfort that, on average, a b<strong>in</strong>ary search tree with n keys generated from a<br />

r<strong>and</strong>om series of <strong>in</strong>sertions <strong>and</strong> removals of keys has expected height O(logn).<br />

Such a statement requires careful mathematical language to precisely def<strong>in</strong>e what<br />

we mean by a r<strong>and</strong>om series of <strong>in</strong>sertions <strong>and</strong> removals, <strong>and</strong> sophisticated<br />

probability theory to prove; hence, its justification is beyond the scope of this<br />

book. Nevertheless, keep <strong>in</strong> m<strong>in</strong>d the poor worst-case performance <strong>and</strong> take care<br />

<strong>in</strong> us<strong>in</strong>g st<strong>and</strong>ard b<strong>in</strong>ary search trees <strong>in</strong> applications where updates are not<br />

r<strong>and</strong>om. There are, after all, applications where it is essential to have a dictionary<br />

with fast worst-case search <strong>and</strong> update times. The data structures presented <strong>in</strong> the<br />

next sections address this need.<br />

10.1.3 <strong>Java</strong> Implementation<br />

In Code Fragments 10.3 through 10.5, we describe a b<strong>in</strong>ary search tree class,<br />

B<strong>in</strong>arySearchTree, which stores objects of class BSTEntry (implement<strong>in</strong>g<br />

the Entry <strong>in</strong>terface) at its nodes. Class B<strong>in</strong>arySearchTree extends class<br />

L<strong>in</strong>ked B<strong>in</strong>aryTree from Code Fragments 7.16 through 7.18, thus tak<strong>in</strong>g<br />

advantage of code reuse.<br />

This class makes use of several auxiliary methods to do much of the heavy lift<strong>in</strong>g.<br />

The auxiliary method treeSearch, based on the TreeSearch algorithm (Code<br />

Fragment 10.1), is <strong>in</strong>voked by the f<strong>in</strong>d, f<strong>in</strong>dAll, <strong>and</strong> <strong>in</strong>sert methods. We<br />

use a recursive addAll method as the ma<strong>in</strong> eng<strong>in</strong>e for the f<strong>in</strong>dAll(k) method, <strong>in</strong><br />

that it performs an <strong>in</strong>order traversal of all the entries with keys equal to k (although<br />

not us<strong>in</strong>g the fast algorithm, s<strong>in</strong>ce it performs a failed search for every entry it<br />

f<strong>in</strong>ds). We use two additional update methods, <strong>in</strong>sertAtExternal, which<br />

<strong>in</strong>serts a new entry at an external node, <strong>and</strong> removeExternal, which removes an<br />

external node <strong>and</strong> its parent.<br />

592

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

Saved successfully!

Ooh no, something went wrong!