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.

Whether <strong>in</strong>ternal nodes of a multi-way tree have two children or many, however,<br />

there is an <strong>in</strong>terest<strong>in</strong>g relationship between the number of entries <strong>and</strong> the number<br />

of external nodes.<br />

Proposition 10.7: An n-entry multi-way search tree has n+1 external<br />

nodes.<br />

We leave the justification of this proposition as an exercise (C-10.14).<br />

Search<strong>in</strong>g <strong>in</strong> a Multi-Way Tree<br />

Given a multi-way search tree T, we note that search<strong>in</strong>g for an entry with key k is<br />

simple. We perform such a search by trac<strong>in</strong>g a path <strong>in</strong> T start<strong>in</strong>g at the root. (See<br />

Figure 10.19b <strong>and</strong> c.) When we are at a d-node v dur<strong>in</strong>g this search, we compare<br />

the key k with the keys k 1 ,…, k d − 1 stored at v. If k = k i for some i, the search is<br />

successfully completed. Otherwise, we cont<strong>in</strong>ue the search <strong>in</strong> the child vi of v<br />

such that k i − 1 ≤ k ≤ k i . (Recall that we conventionally def<strong>in</strong>e k 0 = - ∞ <strong>and</strong> k d =<br />

+∞.) If we reach an external node, then we know that there is no entry with key k<br />

<strong>in</strong> T, <strong>and</strong> the search term<strong>in</strong>ates unsuccessfully.<br />

<strong>Data</strong> <strong>Structures</strong> for Represent<strong>in</strong>g Multi-way Search<br />

Trees<br />

In Section 7.1.3, we discuss a l<strong>in</strong>ked data structure for represent<strong>in</strong>g a general tree.<br />

This representation can also be used for a multi-way search tree. In fact, <strong>in</strong> us<strong>in</strong>g a<br />

general tree to implement a multi-way search tree, the only additional <strong>in</strong>formation<br />

that we need to store at each node is the set of entries (<strong>in</strong>clud<strong>in</strong>g keys) associated<br />

with that node. That is, we need to store with v a reference to some collection that<br />

stores the entries for v.<br />

Recall that when we use a b<strong>in</strong>ary search tree to represent an ordered dictionary D,<br />

we simply store a reference to a s<strong>in</strong>gle entry at each <strong>in</strong>ternal node. In us<strong>in</strong>g a<br />

multi-way search tree T to represent D, we must store a reference to the ordered<br />

set of entries associated with v at each <strong>in</strong>ternal node v of T. This reason<strong>in</strong>g may at<br />

first seem like a circular argument, s<strong>in</strong>ce we need a representation of an ordered<br />

dictionary to represent an ordered dictionary. We can avoid any circular<br />

arguments, however, by us<strong>in</strong>g the bootstrapp<strong>in</strong>g technique, where we use a<br />

previous (less advanced) solution to a problem to create a new (more advanced)<br />

solution. In this case, bootstrapp<strong>in</strong>g consists of represent<strong>in</strong>g the ordered set<br />

associated with each <strong>in</strong>ternal node us<strong>in</strong>g a dictionary data structure that we have<br />

previously constructed (for example, a search table based on a sorted array, as<br />

shown <strong>in</strong> Section 9.3.3). In particular, assum<strong>in</strong>g we already have a way of<br />

implement<strong>in</strong>g ordered dictionaries, we can realize a multi-way search tree by<br />

tak<strong>in</strong>g a tree T <strong>and</strong> stor<strong>in</strong>g such a dictionary at each node of T.<br />

630

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

Saved successfully!

Ooh no, something went wrong!