Held: Friday, 30 April 2004
Today we consider another central ADT, the Dictionary.
Dictionaries are, in effect, arrays indexed by arbitrary values.
- Are there questions on Homework 8?
- Hmmm ... each of my classes has preregistration of 13. How lucky is that?
- The picnic may be in the elbow today. Decision by 1 p.m.
- Introduction to the Dictionary ADT
- Dictionaries vs. Databases.
- Implementation: Association Lists (and variants).
- Implementation: Binary Search Trees.
- In our investigation of vectors and arrays, we've seen structures that
collect objects in which the objects can be indexed by numbers.
- Are there other ways to index collections? Yes, we could index
collections by strings.
- We could also index by other objects (Strings, Integers, whatever).
- Collections of objects indexed by objects are called dictionaries.
- Some people also refer to them as keyed tables.
- Some more terminology:
- The objects we're using as indices are called keys.
- The objects we're looking up are called values.
- Why do we do we want dictionaries? They make many tasks more
- Keeping track of objects we've seen. If an object is an index in
the dictionary, we've seen it; if it's not an index in the dictionary,
we haven't seen it. This would be helpful for our puzzle solver.
- Counting things We can use the dictionary to associate a
counter with each object; when you see another copy, increment the
- Databases. There are a number of techniques for implementing and thinking
about databases. Dictionaries provide a simple mechanism for
- How do we do implement dictionaries? There are a number of techniques,
including linked lists of key/value pairs.
and won't worry about how they work. Later, we'll look at how to build
- What features do we need in a dictionary? Some are similar to those
we need for vectors.
- Add something to the dictionary (requires a key and value).
- Look up something in the dictionary (requires a key, returns a value).
- Remove something from the dictionary.
- Others correspond more to the different structure of dictionaries.
- List/iterate all the keys in the dictionary.
- List/iterate all the values in the dictionary.
- Note that dictionaries are not the same as databases.
- Why not? There are a number of reasons.
- Dictionaries have a single key, and that key provides the only
way to access information.
- Databases may have multiple keys. You may also access information
using other fields. For example, while a database may be keyed by
ID#, you might still be able to search by name.
- A dictionary returns a single object given a request.
- A database may return multiple objects (e.g., all the students with
- Use a simple
KeyedValue structure (that stores a
key and a value).
- Shove 'em in a list.
- Search through the list step by step.
- Other variants:
- Use a vector.
- Keep the values ordered.
- We can also apply the divide-and-conquer strategy to this data
- That is, we'll put the values in a tree.
- Here's a way of making the search potentially more efficient: The BST property: The keys of all left descendants are less than the key of the root; The keys of all right decendants are greater than the key of the root.
- If the tree is balanced, adding and searching are O(logn)
- But trees can be easily unbalanced.
- Keeping trees balanced is a topic for CSC301.