12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

140 Chap. 4 Lists, Stacks, <strong>and</strong> Queues/** The Dictionary abstract class. */public interface Dictionary {/** Reinitialize dictionary */public void clear();/** Insert a record@param k The key for the record being inserted.@param e The record being inserted. */public void insert(Key k, E e);/** Remove <strong>and</strong> return a record.@param k The key of the record <strong>to</strong> be removed.@return A maching record. If multiple records match"k", remove an arbitrary one. Return null if no recordwith key "k" exists. */public E remove(Key k);/** Remove <strong>and</strong> return an arbitrary record from dictionary.@return the record removed, or null if none exists. */public E removeAny();/** @return A record matching "k" (null if none exists).If multiple records match, return an arbitrary one.@param k The key of the record <strong>to</strong> find */public E find(Key k);/** @return the number of records in the dictionary. */public int size();};Figure 4.27 The ADT for a simple dictionary.records in sorted order in an array, which permits a binary search. Fortunately, inpractice most fields of most records consist of simple data types with natural <strong>to</strong>talorders. For example, integers, floats, doubles, <strong>and</strong> character strings all are <strong>to</strong>tallyordered. Ordering fields that are naturally multi-dimensional, such as a point in twoor three dimensions, present special opportunities if we wish <strong>to</strong> take advantage oftheir multidimensional nature. This problem is addressed in Section 13.3.Figure 4.27 shows the definition for a simple abstract dictionary class. Themethods insert <strong>and</strong> find are the heart of the class. Method insert takes arecord <strong>and</strong> inserts it in<strong>to</strong> the dictionary. Method find takes a key value <strong>and</strong> returnssome record from the dictionary whose key matches the one provided. If there aremultiple records in the dictionary with that key value, there is no requirement as <strong>to</strong>which one is returned.

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

Saved successfully!

Ooh no, something went wrong!