[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Project] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Exams] [Examples] [Book] [Tutorial] [API]
Back to Dictionaries. On to Hash Tables.
Held Thursday, November 11, 1999
Overview
Today we continue our discussion of dictionaries with a few possible implementation strategies.
Notes
Contents
Summary
/**
* Very simple dictionaries that map keys to values.
*
* @author Samuel A. Rebelsky
* @version 1.0 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 put(Object key, Object value)
throws Exception;
/**
* 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 get(Object key)
throws Exception;
} // interface Dictionary
Pair object that joins a
key and value.
/**
* Nodes in a binary search tree. It is expected that you will
* access the fields directly, as this is only used as a helper
* for other classes.
*
* @author Samuel A. Rebelsky
* @version 1.1 of November 1999
*/
public class BinarySearchNode
{
// +--------+--------------------------------------------------
// | Fields |
// +--------+
/** The key in the node (used for ordering the tree) . */
Object key;
/** The value stored in the node. */
Object value;
/** A linked structure of things smaller than this node. */
BinarySearchNode smaller;
/** A linked structure of things larger than this node. */
BinarySearchNode larger;
// +--------------+--------------------------------------------
// | Constructors |
// +--------------+
/**
* Build a single node in the tree.
*/
public BinarySearchNode(Object key, Object value) {
this.key = key;
this.value = value;
} // BinarySearchNode(Object,Object)
} // class BinarySearchNode
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.
/**
* A simple implementation of binary search trees.
*
* @author Samuel A. Rebelsky
* @version 1.0 of November 1999
*/
public class BinarySearchTree
implements Dictionary
{
// +--------+--------------------------------------------------
// | Fields |
// +--------+
/** The root of the binary search tree. */
BinarySearchNode root;
/** The comparator used to order the keys in the search tree. */
Comparator compare;
// +--------------+--------------------------------------------
// | Constructors |
// +--------------+
/**
* Build a new binary search tree whose nodes are ordered
* by a particular comparator.
*/
public BinarySearchTree(Comparator compare) {
this.compare = compare;
this.root = null;
} // BinarySearchTree(Comparator)
// +---------+-------------------------------------------------
// | Methods |
// +---------+
/**
* Find a value in the tree.
*/
public Object get(Object key)
throws Exception
{
return get(root, key);
} // get(Object)
/**
* Add a value to the tree.
*/
public void put(Object key, Object value)
throws Exception
{
root = insert(root, key, value);
} // put(Object)
/**
* Convert to a string for printing.
*/
public String toString() {
return toString(root, "");
} // toString()
// +---------+-------------------------------------------------
// | Helpers |
// +---------+
/**
* Find a value in a subtree.
*/
public Object get(BinarySearchNode root, Object key)
throws Exception
{
// If the tree is empty, we're done.
if (root == null) {
throw new Exception("'" + key.toString() + "' is not in the tree");
}
// If the key is at the root, we're done
else if (compare.equals(root.key, key)) {
return root.value;
}
// If the key is smaller, try the left subtree
else if (compare.lessThan(key, root.key)) {
return get(root.smaller, key);
}
// If the key is larger, try the right subtree
else {
return get(root.larger, key);
}
} // get(BinarySearchNode, Object)
/**
* Insert a value into a node-based tree, updating the tree as
* you go.
*/
public BinarySearchNode insert(BinarySearchNode root,
Object key, Object value)
throws Exception
{
// To insert into the empty tree, make a new tree.
if (root == null) {
return new BinarySearchNode(key,value);
}
// If the keys are the same, update
else if (compare.equals(root.key, key)) {
root.value = value;
}
// If the key to be inserted is smaller, insert into the left subtree
else if (compare.lessThan(key, root.key)) {
root.smaller = insert(root.smaller, key, value);
}
// Otherwise, insert into the right subtree
else {
root.larger = insert(root.larger, key, value);
}
return root;
} // insert(BinarySearchNode, Object, Object)
/**
* Convert to a simple, indented, string for printing.
* Uses a multi-line representation, in which subtrees are further
* indented. Note that the "indent" parameter is used for indenting
* subtrees, not the current tree.
*/
public String toString(BinarySearchNode root, String indent) {
if (root == null) return "*\n";
else return root.value.toString() + "\n"
+ indent + " <-" + toString(root.smaller, indent + " | ")
+ indent + " >-" + toString(root.larger, indent + " ")
+ indent + "\n";
} // toString()
} // class BinarySearchTree
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, 10 August 1999
Wednesday, 10 November 1999
Dictionary interface.
Back to Dictionaries. On to Hash Tables.
[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Project] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Exams] [Examples] [Book] [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/99F/Outlines/outline.41.html
Source text last modified Wed Nov 10 20:58:01 1999.
This page generated on Wed Nov 10 21:01:54 1999 by Siteweaver. Validate this page's HTML.
Contact our webmaster at rebelsky@grinnell.edu