04.02.2018 Views

Algorithms

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

488 CHAPTER 3 ■ Searching<br />

Our symbol-table implementations are useful for a wide range of applications, but<br />

our algorithms are easily adapted to support several other options that are widely used<br />

and worth considering.<br />

Pr imit ive t y p e s . Suppose that we have a symbol table with integer keys and associated<br />

floating-point numbers. When we use our standard setup, the keys and values<br />

are stored as Integer and Double wrapper-type values, so we need two extra memory<br />

references to access each key-value pair. These references may be no problem in an application<br />

that involves thousands of searches on thousands of keys but may represent<br />

excessive cost in an application that involves billions of searches on millions of keys. Using<br />

a primitive type instead of Key would save one reference per key-value pair. When<br />

the associated value is also primitive, we can eliminate another reference. The situation<br />

is diagrammed at right for separate chaining; the<br />

same tradeoffs hold for other implementations. For<br />

performance-critical applications, it is worthwhile<br />

and not difficult to develop versions of our implementations<br />

along these lines (see Exercise 3.5.4).<br />

Duplicate ke y s . The possibility of duplicate keys<br />

sometimes needs special consideration in symboltable<br />

implementations. In many applications, it is<br />

desirable to associate multiple values with the same<br />

key. For example, in a transaction-processing system,<br />

numerous transactions may have the same<br />

customer key value. Our convention to disallow<br />

duplicate keys amounts to leaving duplicate-key<br />

management to the client. We will consider an example<br />

of such a client later in this section. In many<br />

of our implementations, we could consider the alternative<br />

of leaving key-value pairs with duplicate<br />

standard implementation<br />

data is stored in<br />

Key and Value objects<br />

primitive-type implementation<br />

data is stored in<br />

linked-list nodes<br />

Memory usage for separate chaining<br />

keys in the primary search data structure and to return any value with the given key for<br />

a search. We might also add methods to return all values with the given key. Our BST<br />

and hashing implementations are not difficult to adapt to keep duplicate keys within<br />

the data structure; doing so for red-black BSTs is just slightly more challenging (see Exercise<br />

3.5.9 and Exercise 3.5.10). Such implementations are common in the literature<br />

(including earlier editions of this book).

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

Saved successfully!

Ooh no, something went wrong!