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 135/** Container class for a key-value pair */class KVpair {priv<strong>at</strong>e Key k;priv<strong>at</strong>e E e;}/** Constructors */KVpair(){ k = null; e = null; }KVpair(Key kval, E eval){ k = kval; e = eval; }/** <strong>D<strong>at</strong>a</strong> member access functions */public Key key() { return k; }public E value() { return e; }Figure 4.32 Implement<strong>at</strong>ion for a class representing a key-value pair.The fundamental issue is th<strong>at</strong> the key value for a record is not an intrinsic propertyof the record’s class, or of any field within the class. The key for a record isactually a property of the context in which the record is used.A truly general altern<strong>at</strong>ive is to explicitly store the key associ<strong>at</strong>ed with a givenrecord, as a separ<strong>at</strong>e field in the dictionary. Th<strong>at</strong> is, each entry in the dictionarywill contain both a record <strong>and</strong> its associ<strong>at</strong>ed key. Such entries are known as keyvaluepairs. It is typical th<strong>at</strong> storing the key explicitly duplic<strong>at</strong>es some field in therecord. However, keys tend to be much smaller than records, so this additionalspace overhead will not be gre<strong>at</strong>. A simple class for representing key-value pairsis shown in Figure 4.32. The insert method of the dictionary class supports thekey-value pair implement<strong>at</strong>ion because it takes two parameters, a record <strong>and</strong> itsassoci<strong>at</strong>ed key for th<strong>at</strong> dictionary.Now th<strong>at</strong> we have defined the dictionary ADT <strong>and</strong> settled on the design approachof storing key-value pairs for our dictionary entries, we are ready to considerways to implement it. Two possibilities would be to use an array-based or linkedlist. Figure 4.33 shows an implement<strong>at</strong>ion for the dictionary using an (unsorted)array-based list.Examining class UALdict (UAL st<strong>and</strong>s for “unsorted array-based list), we caneasily see th<strong>at</strong> insert is a constant-time oper<strong>at</strong>ion, because it simply inserts thenew record <strong>at</strong> the end of the list. However, find, <strong>and</strong> remove both require Θ(n)time in the average <strong>and</strong> worst cases, because we need to do a sequential search.Method remove in particular must touch every record in the list, because once thedesired record is found, the remaining records must be shifted down in the list tofill the gap. Method removeAny removes the last record from the list, so this is aconstant-time oper<strong>at</strong>ion.on the keyword list triggered this appearance of the record. Thus, we cannot write a function th<strong>at</strong>extracts the key from such a record.

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

Saved successfully!

Ooh no, something went wrong!