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 137}/** @return List size */public int size(){ return list.length(); }Figure 4.33 (continued)As an altern<strong>at</strong>ive, we could implement the dictionary using a linked list. Theimplement<strong>at</strong>ion would be quite similar to th<strong>at</strong> shown in Figure 4.33, <strong>and</strong> the costof the functions should be the same asymptotically.Another altern<strong>at</strong>ive would be to implement the dictionary with a sorted list. Theadvantage of this approach would be th<strong>at</strong> we might be able to speed up the findoper<strong>at</strong>ion by using a binary search. To do so, first we must define a vari<strong>at</strong>ion onthe List ADT to support sorted lists. A sorted list is somewh<strong>at</strong> different froman unsorted list in th<strong>at</strong> it cannot permit the user to control where elements getinserted. Thus, the insert method must be quite different in a sorted list than inan unsorted list. Likewise, the user cannot be permitted to append elements ontothe list. For these reasons, a sorted list cannot be implemented with straightforwardinheritance from the List ADT.The cost for find in a sorted list is Θ(log n) for a list of length n. This is agre<strong>at</strong> improvement over the cost of find in an unsorted list. Unfortun<strong>at</strong>ely, thecost of insert changes from constant time in the unsorted list to Θ(n) time inthe sorted list. Whether the sorted list implement<strong>at</strong>ion for the dictionary ADT ismore or less efficient than the unsorted list implement<strong>at</strong>ion depends on the rel<strong>at</strong>ivenumber of insert <strong>and</strong> find oper<strong>at</strong>ions to be performed. If many more findoper<strong>at</strong>ions than insert oper<strong>at</strong>ions are used, then it might be worth using a sortedlist to implement the dictionary. In both cases, remove requires Θ(n) time in theworst <strong>and</strong> average cases. Even if we used binary search to cut down on the time tofind the record prior to removal, we would still need to shift down the remainingrecords in the list to fill the gap left by the remove oper<strong>at</strong>ion.Given two keys, we have not properly addressed the issue of how to comparethem. One possibility would be to simply use the basic ==, = oper<strong>at</strong>orsbuilt into Java. This is the approach taken by our implement<strong>at</strong>ions for dictionariesshown in Figure 4.33. If the key type is int, for example, this will workfine. However, if the key is a pointer to a string or any other type of object, thenthis will not give the desired result. When we compare two strings we probablywant to know which comes first in alphabetical order, but wh<strong>at</strong> we will get fromthe st<strong>and</strong>ard comparison oper<strong>at</strong>ors is simply which object appears first in memory.Unfortun<strong>at</strong>ely, the code will compile fine, but the answers probably will not be fine.In a language like C++ th<strong>at</strong> supports oper<strong>at</strong>or overloading, we could requireth<strong>at</strong> the user of the dictionary overload the ==, = oper<strong>at</strong>ors for the givenkey type. This requirement then becomes an oblig<strong>at</strong>ion on the user of the dictionary

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

Saved successfully!

Ooh no, something went wrong!