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

Create successful ePaper yourself

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

Sec. 4.4 Dictionaries 1314.3.3 Comparison of Array-Based <strong>and</strong> Linked QueuesAll member functions for both the array-based <strong>and</strong> linked queue implement<strong>at</strong>ionsrequire constant time. The space comparison issues are the same as for the equivalentstack implement<strong>at</strong>ions. Unlike the array-based stack implement<strong>at</strong>ion, there isno convenient way to store two queues in the same array, unless items are alwaystransferred directly from one queue to the other.4.4 DictionariesThe most common objective of computer programs is to store <strong>and</strong> retrieve d<strong>at</strong>a.Much of this book is about efficient ways to organize collections of d<strong>at</strong>a recordsso th<strong>at</strong> they can be stored <strong>and</strong> retrieved quickly. In this section we describe asimple interface for such a collection, called a dictionary. The dictionary ADTprovides oper<strong>at</strong>ions for storing records, finding records, <strong>and</strong> removing records fromthe collection. This ADT gives us a st<strong>and</strong>ard basis for comparing various d<strong>at</strong>astructures.Before we can discuss the interface for a dictionary, we must first define theconcepts of a key <strong>and</strong> comparable objects. If we want to search for a given recordin a d<strong>at</strong>abase, how should we describe wh<strong>at</strong> we are looking for? A d<strong>at</strong>abase recordcould simply be a number, or it could be quite complic<strong>at</strong>ed, such as a payroll recordwith many fields of varying types. We do not want to describe wh<strong>at</strong> we are lookingfor by detailing <strong>and</strong> m<strong>at</strong>ching the entire contents of the record. If we knew everythingabout the record already, we probably would not need to look for it. Instead,we typically define wh<strong>at</strong> record we want in terms of a key value. For example, ifsearching for payroll records, we might wish to search for the record th<strong>at</strong> m<strong>at</strong>chesa particular ID number. In this example the ID number is the search key.To implement the search function, we require th<strong>at</strong> keys be comparable. At aminimum, we must be able to take two keys <strong>and</strong> reliably determine whether theyare equal or not. Th<strong>at</strong> is enough to enable a sequential search through a d<strong>at</strong>abaseof records <strong>and</strong> find one th<strong>at</strong> m<strong>at</strong>ches a given key. However, we typically wouldlike for the keys to define a total order (see Section 2.1), which means th<strong>at</strong> wecan tell which of two keys is gre<strong>at</strong>er than the other. Using key types with totalorderings gives the d<strong>at</strong>abase implementor the opportunity to organize a collectionof records in a way th<strong>at</strong> makes searching more efficient. An example is storing therecords in sorted order in an array, which permits a binary search. Fortun<strong>at</strong>ely, inpractice most fields of most records consist of simple d<strong>at</strong>a types with n<strong>at</strong>ural totalorders. For example, integers, flo<strong>at</strong>s, doubles, <strong>and</strong> character strings all are totallyordered. Ordering fields th<strong>at</strong> are n<strong>at</strong>urally multi-dimensional, such as a point in twoor three dimensions, present special opportunities if we wish to take advantage oftheir multidimensional n<strong>at</strong>ure. This problem is addressed in Section 13.3.

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

Saved successfully!

Ooh no, something went wrong!