11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

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.

Sec. 4.4 Dictionaries 133not get <strong>at</strong> a record of the dictionary th<strong>at</strong> he didn’t already know the key value for.With the removeAny method, the user can process all records in the dictionary asshown in the following code fragment.while (dict.size() > 0) {it = dict.removeAny();doSomething(it);}There are other approaches th<strong>at</strong> might seem more n<strong>at</strong>ural for iter<strong>at</strong>ing though adictionary, such as using a “first” <strong>and</strong> a “next” function. But not all d<strong>at</strong>a structuresth<strong>at</strong> we want to use to implement a dictionary are able to do “first” efficiently. Forexample, a hash table implement<strong>at</strong>ion cannot efficiently loc<strong>at</strong>e the record in thetable with the smallest key value. By using RemoveAny, we have a mechanismth<strong>at</strong> provides generic access.Given a d<strong>at</strong>abase storing records of a particular type, we might want to searchfor records in multiple ways. For example, we might want to store payroll recordsin one dictionary th<strong>at</strong> allows us to search by ID, <strong>and</strong> also store those same recordsin a second dictionary th<strong>at</strong> allows us to search by name.Figure 4.30 shows an implement<strong>at</strong>ion for a payroll record. Class Payroll hasmultiple fields, each of which might be used as a search key. Simply by varyingthe type for the key, <strong>and</strong> using the appropri<strong>at</strong>e field in each record as the key value,we can define a dictionary whose search key is the ID field, another whose searchkey is the name field, <strong>and</strong> a third whose search key is the address field. Figure 4.31shows an example where Payroll objects are stored in two separ<strong>at</strong>e dictionaries,one using the ID field as the key <strong>and</strong> the other using the name field as the key.The fundamental oper<strong>at</strong>ion for a dictionary is finding a record th<strong>at</strong> m<strong>at</strong>ches agiven key. This raises the issue of how to extract the key from a record. We wouldlike any given dictionary implement<strong>at</strong>ion to support arbitrary record types, so weneed some mechanism for extracting keys th<strong>at</strong> is sufficiently general. One approachis to require all record types to support some particular method th<strong>at</strong> returns the keyvalue. For example, in Java the Comparable interface can be used to provide thiseffect. Unfortun<strong>at</strong>ely, this approach does not work when the same record type ismeant to be stored in multiple dictionaries, each keyed by a different field of therecord. This is typical in d<strong>at</strong>abase applic<strong>at</strong>ions. Another, more general approachis to supply a class whose job is to extract the key from the record. Unfortun<strong>at</strong>ely,this solution also does not work in all situ<strong>at</strong>ions, because there are record types forwhich it is not possible to write a key extraction method. 22 One example of such a situ<strong>at</strong>ion occurs when we have a collection of records th<strong>at</strong> describe booksin a library. One of the fields for such a record might be a list of subject keywords, where the typicalrecord stores a few keywords. Our dictionary might be implemented as a list of records sorted bykeyword. If a book contains three keywords, it would appear three times on the list, once for eachassoci<strong>at</strong>ed keyword. However, given the record, there is no simple way to determine which keyword

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

Saved successfully!

Ooh no, something went wrong!