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.

10.1 B<strong>in</strong>ary Search Trees<br />

All of the structures we discuss <strong>in</strong> this chapter are search trees, that is, tree data<br />

structures that can be used to implement a dictionary. Let us, therefore, beg<strong>in</strong> by<br />

briefly review<strong>in</strong>g the fundamental methods of the dictionary ADT:<br />

• f<strong>in</strong>d(k): Return an entry with key k, if it exists.<br />

• f<strong>in</strong>dAll(k): Return an iterable collection of all entries with keys equal to k.<br />

• <strong>in</strong>sert(k,x): Insert an entry with key k <strong>and</strong> value x.<br />

• remove(e): Remove an entry e, <strong>and</strong> return it.<br />

• removeAll(k): Remove all entries with key k, return<strong>in</strong>g an iterator of their<br />

values.<br />

Method f<strong>in</strong>d returns null if k is not found. The ordered dictionary ADT <strong>in</strong>cludes<br />

some additional methods for search<strong>in</strong>g through predecessors <strong>and</strong> successors of a key<br />

or entry, but their performance is similar to that of f<strong>in</strong>d. So we will be focus<strong>in</strong>g on<br />

f<strong>in</strong>d as the primary search operation <strong>in</strong> this chapter.<br />

B<strong>in</strong>ary trees are an excellent data structure for stor<strong>in</strong>g the entries of a dictionary,<br />

assum<strong>in</strong>g we have an order relation def<strong>in</strong>ed on the keys. As mentioned previously<br />

(Section 7.3.6), a b<strong>in</strong>ary search tree is a b<strong>in</strong>ary tree T such that each <strong>in</strong>ternal node v<br />

of T stores an entry (k,x) such that:<br />

• Keys stored at nodes <strong>in</strong> the left subtree of v are less than or equal to k.<br />

• Keys stored at nodes <strong>in</strong> the right subtree of v are greater than or equal to k.<br />

As we show below, the keys stored at the nodes of T provide a way of perform<strong>in</strong>g a<br />

search by mak<strong>in</strong>g a comparison at each <strong>in</strong>ternal node v, which can stop at v or<br />

cont<strong>in</strong>ue at v's left or right child. Thus, we take the view here that b<strong>in</strong>ary search trees<br />

are nonempty proper b<strong>in</strong>ary trees. That is, we store entries only at the <strong>in</strong>ternal nodes<br />

of a b<strong>in</strong>ary search tree, <strong>and</strong> the external nodes serve only as "placeholders." This<br />

approach simplifies several of our search <strong>and</strong> update algorithms. Incidentally, we<br />

could have allowed for improper b<strong>in</strong>ary search trees, which have better space usage,<br />

but at the expense of more complicated search <strong>and</strong> update methods.<br />

Independent of whether we view b<strong>in</strong>ary search trees as proper or not, the important<br />

property of a b<strong>in</strong>ary search tree is the realization of an ordered dictionary (or map).<br />

That is, a b<strong>in</strong>ary search tree should hierarchically represent an order<strong>in</strong>g of its keys,<br />

us<strong>in</strong>g relationships between parent <strong>and</strong> children. Specifically, an <strong>in</strong>order traversal<br />

(Section 7.3.6) of the nodes of a b<strong>in</strong>ary search tree T should visit the keys <strong>in</strong><br />

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

584

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

Saved successfully!

Ooh no, something went wrong!