[Instructions] [Search] [Current] [News] [Syllabus] [Glance] [Links] [Handouts] [Project] [Outlines] [Labs] [Assignments] [Quizzes] [Exams] [Examples] [EIJ] [JPDS] [Tutorial] [API]
Back to Dictionaries. On to Hash Tables.
Held Tuesday, April 18, 2000
Overview
Today we consider two simple implementations of dictionaries: association lists and binary search trees.
Notes
Contents
Summary
/** * Very simple dictionaries that map keys to values. * * @author Samuel A. Rebelsky * @version 1.1 of November 1999 */ public interface Dictionary { /** * Add an entry to the dictionary. * Pre: The key and value are non-null. * Pre: The key can be compared to other values in the dictionary. * Post: Subsequent calls to get(key) will return value. * * @exception Exception * If the key cannot be compared. */ public void add(Object key, Object value) throws Exception; /** * Determine if a particular key is in the dictionary. * Pre: None. * Post: Returns true if the key is in the dictionary; * Returns false otherwise. */ public boolean contains(Object key); /** * Look up an entry in the dictionary. * Pre: The key is non-null. * Pre: The key can be compared to other values in the dictionary. * Post: Returns the value most recently "put" with the key. * * @exception Exception * If the key cannot be compared or there is no such entry. */ public Object lookup(Object key) throws Exception; /** * Get a list of all the keys in the dictionary. * Pre: (None) * Post: Returns a list of all the keys. */ public SimpleList getKeys(); } // interface Dictionary
Pair
object that joins a
key and value.
D / \ B F / \ / \ A C E G
Rebelsky / \ Ferguson Walker / \ / \ Chamberland Herman Stone Wolf / / \ Adelberg Flynt Jepsen / \ Hill MooreE \ MooreT
Dictionary
with binary search
trees, we'll build a wrapper class. This class will permit us to
Comparator
used to determine
large and small.
public interface BinarySearchable { /** * Get the ``middle'' key in the collection. * Pre: The collection is nonempty. * Post: Returns some designated element in the collection. */ public Object middle(); /** * Get elements in the collection that are smaller than the * middle element. * Pre: The collection is nonempty. * Pre: The collection has not been modified since the last call * to middle. * Post: Returns the smaller elements. */ public BinarySearchable smaller(); /** * Get elements in the collection that are larger than the * middle element. * Pre: The collection is nonempty. * Pre: The collection has not been modified since the last call * to middle. * Post: Returns the larger elements. */ public BinarySearchable larger(); /** * Determine if the collection is empty. */ public boolean isEmpty(); } // BinarySearchable
public boolean binarySearch(BinarySearchable stuff, Object findMe, Comparator compare) { if (stuff.isEmpty()) return false; else if (compare.equals(findMe, stuff.middle())) return true; else if (compare.lessThan(findMe, stuff.middle())) return binarySearch(stuff.smaller(), findMe, compare); else return binarySearch(stuff.larger(), findMe, compare); } // binarySearch(BinarySearchable, Object, Comparator)
Tuesday, 18 January 2000
Tuesday, 18 April 2000
Back to Dictionaries. On to Hash Tables.
[Instructions] [Search] [Current] [News] [Syllabus] [Glance] [Links] [Handouts] [Project] [Outlines] [Labs] [Assignments] [Quizzes] [Exams] [Examples] [EIJ] [JPDS] [Tutorial] [API]
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.
This page may be found at http://www.math.grin.edu/~rebelsky/Courses/CS152/2000S/Outlines/outline.42.html
Source text last modified Tue Apr 18 09:58:25 2000.
This page generated on Tue Apr 18 09:59:20 2000 by Siteweaver. Validate this page's HTML.
Contact our webmaster at rebelsky@grinnell.edu