Fundamentals of Computer Science II (CSC-152 97F)
Outline of Class 45: Dictionaries
- The Fourth Annual Consortium for Computing in Small
Colleges Central Plains Conference is having a Student Web Page
contest. You can find more information at
- I've decided to make
completely optional. That is, don't worry about
doing it unless you feel you'll derive significant benefit or would
like to ameliorate past mistakes.
- This class outline is a little rougher than most since I had a lot
of extra stuff to do. The same may be true of tomorrow's outline.
- After some discussion with other faculty, I'm skipping ahead to
Chapter 14, Dictionaries. Try to read Chapter 15 over break.
- Don't forget Tuesday's "Math Profs were once Student Math Junkies"
- There was some question as to whether the heap operations we
discussed really worked, particularly whether or not they were
correct independent of which branch you were on.
- Hence, we'll attempt a more complex
example, based on sorting a list of ten or so numbers (more would
be difficult, fewer wouldn't give us enough levels in the tree).
- Recall that insertion requires swapping up, and deletion requires
- We've seen a number of different structures that provide a number
of different properties, mostly relating to policies for inserting
and deleting elements and their related costs.
- Array permit position-based insertion and deletion at unit cost.
- Vectors permit position-based insertion and deletion at
cost potentially linear in the number of elements.
at unit cost.
- List permit insertion and deletion of the first element at unit cost
and other types of modification at other costs.
- Linear structures typically permit insertion and deletion at
"some position" for unit cost.
- Heaps permit insertion of any element and deletion of least element
at logarithmic cost.
- There are, however, other actions we might consider. These include
- Membership: is an element in a set?
- Associate a value with a key.
- Determine which value is associated with a key.
- Modeling: given this real-world situation, how might
we model it on the computer and manipulate that model to better
understand the original situation.
- Let's focus on the association actions mentioned above.
- They might be deemed a form of information retrieval, since
we need to retrieve information associated with an object.
- Note that there are a number of common problems that might be
addressed by such a structure:
- Grading: Finding the grades associated with a student.
- Retrieval: Looking up the positions of a word in a document
(based on an index of that document).
- Writing: Finding the definition of a word.
- It is this l ast use that leads us to the typical name of such
structures: the dictionary.
- A dictionary is a structure that supports the following two
public Object insert(Object key, Object value) --
insert a value (definition) into the dictionary, associating
it with a particular key (word). Bailey suggests returning the old
value associated with they key.
public boolean lookup(Object key) -- determine the
value associated with a key.
- There are a number of additional operations one might support, including
- determining whether or not a value is in the dictionary
- determining whether or not a key is in the ditionary
- removing objects based on keys
- removing objects based on values
- getting all the keys from a dictionary
- extending the value associated with a key
- getting all the values in the dictionary
- getting the size of the dictionary
- As with our other structures, the particular methods we choose to
include and the emphasis we place on various methods can significantly
affect our implementation.
- How can we implement dictionaries?
- We could define a
Pair object that joins a
key and value.
- We could make an unordered array of those objects.
- To insert an element, we put it at the end of the array. This
is O(1), but we are limited to a particular number of elements.
- To lookup an element, we step through the pairs one by one, comparing
the key we are searching for to the key in each pair. This is
O(n), where n is the number of elements.
- We could make an ordered array of those objects (an ordered array
being one that is always sorted).
- To insert an element, we put it at the appropriate place, shifting
as necessary. This is O(n) and is again limited by the size of
- For lookup an element, we do binary search on the sorted array.
- We could build a search tree: a binary tree in which all
(the keys of) the elements of the left subtree are less than
or equal to (the key of) the root, and all (the keys of) the elements
of the right subtree are greater than or equal to the (key) of the
- To insert an element, we step through the tree, following left and
right edges as appropriate and then insert it when there is no where
else to go. This is O(depth).
- To find an element, we also step through the tree, following edges
as appropriate until we find the key or fall off the tree. This is
- If we can make the depth small relative to the size of the tree, this
can be significantly more efficient. In particular, a tree of size
n can have depth of O(log_2(n)).
- However, we'll need to find a way to keep trees balanced. That is
a topic for CS302 (or one we may cover if we find we have a little
bit of free time the last week of class).
- Can we do better?
Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
Source text last modified Wed Nov 26 08:20:24 1997.
This page generated on Wed Nov 26 09:29:26 1997 by SiteWeaver.
Contact our webmaster at firstname.lastname@example.org