*This preview shows
pages
1–3. Sign up to
view the full content.*

Computer Science 211
Data Structures
Mount Holyoke College
Fall 2009
Topic Notes: Maps and Hashing
Maps/Dictionaries
A
map
or
dictionary
is a structure used for looking up items via key-value associations.
We have seen this idea several times so far:
•
Way back at the beginning of the semester, when we used a dictionary of
Harry Potter
spells.
•
We stored symbol tables in maps for the postscript lab.
•
We stored our vertices in maps in the graph implementations we just looked at.
Both Java and the structure package define an interface called
Map
that we can use for this.
See Structure Source:
/home/jteresco/shared/cs211/src/structure5/Map.java
This interface include the kinds of things we are used to seeing in our structures, so we won’t spend
too much time looking at it.
There are a lot of implementations of a
Map
possible.
We can use a linked list of
Association
s.
See Structure Source:
/home/jteresco/shared/cs211/src/structure5/MapList.java
We can use a
MapList
to simplify the
Spells
example.
See Example:
/home/jteresco/shared/cs211/examples/Spells/SpellsMapList.java
This provides a convenient implementation, but doesn’t improve efficiency over our previous meth-
ods.
If our keys are comparable, we might keep our map contents sorted for more efficient lookups with
a sorted array, or a binary search tree.
See Structure Source:
/home/jteresco/shared/cs211/src/structure5/Table.java
In fact, there are many possible implementations of maps. If we think abstractly about a few
variants, we can consider their time complexity on common opertions and space complexity. In
the table below,
n
denotes the actual number of elements in the map, and
N
denote the maximum
number of elements it could contain (where that restriction makes sense).

This ** preview**
has intentionally

**sections.**

*blurred***to view the full version.**

*Sign up*CS 211
Data Structures
Fall 2009
Structure
Search
Insert
Delete
Space
Linked List
O
(
n
)
O
(1)
O
(
n
)
O
(
n
)
Sorted Array
O
(log
n
)
O
(
n
)
O
(
n
)
O
(
N
)
Balanced BST
O
(log
n
)
O
(log
n
)
O
(log
n
)
O
(
n
)
Array[KeyRange] of
EltType
O
(1)
O
(1)
O
(1)
KeyRange
That last line is very interesting. If we know the range of keys (suppose they’re all integers between
0 and KeyRange-1) we can use the key as the index directly into an array. And have constant time
access!
Hashing
The map implementation of an array with keys as the subscripts and values as contents makes a lot
of sense. However, there are some important restrictions on the use of this representation.
This implementation assumes that the data has a key which is of a restricted type (some enumerated
type in Pascal, integers in Java), which is not always the case. What if we’re trying to store strings
or doubles or something more complex?
Moreover, the size requirements for this implementation could be prohibitive. Suppose we wanted
to store 2000 student records indexed by social security number. We would need an array with 1
billion elements!
In cases like this, where most of the entries will be empty, we would like to use a smaller array, but

This is the end of the preview. Sign up
to
access the rest of the document.