Computer Science Fundamentals (CS153 2004S)
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[Honesty]
[Instructions]
[Links]
[Search]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]
Misc:
[Experiments in Java]
[Java API]
[Scheme Reference]
[Scheme Report]
[CS153 2003S]
[CS151 2003F]
[CS152 2000F]
[SamR]
Back to Implementing Dictionaries with Hash Tables. On to Discussion of Exam 3.
Held: Wednesday, 5 May 2004
Summary: Today we consider a tree-based implementation of dictionaries, the binary search tree.
Related Pages:
Notes:
Overview:
middleelement.
smallerelements: everything less than the
middleelement.
largerelements: everything greater than the
middleelement.
/**
* Collections that support the key operations for binary search.
*
* @author Samuel A. Rebelsky
* @version 1.1 of May 2004.
*/
public interface BinarySearchable
extends Collection
{
/**
* Get the ``middle'' value in the collection.
* Preconditions:
* (1) The collection is nonempty.
* Postconditions:
* (2) Returns some designated element in the collection. That element
* should be less than approximately half the elements and greater
* than approximately half the elements.
*/
public Object middle();
/**
* Get elements in the collection that are smaller than the
* middle element.
* Preconditions:
* (1) The collection is nonempty.
* Postconditions:
* (1) Each element in the returned collection is smaller than or equal
* to the value returned by middle.
* (2) Any element in the original collection that is smaller than or
* equal to the value returned by middle is in the returned collection.
*/
public BinarySearchable smaller();
/**
* Get elements in the collection that are larger than the
* middle element.
* Preconditions:
* (1) The collection is nonempty.
* Postconditions:
* (1) Each element in the returned collection is greater than or equal
* to the value returned by middle.
* (2) Any element in the original collection that is greater than or
* equal to the value returned by middle is in the returned collection.
*/
public BinarySearchable larger();
/**
* Determine if the collection is empty.
*/
public boolean isEmpty();
} // BinarySearchable
public boolean binarySearch(BinarySearchable stuff,
Object findMe,
Comparator order)
{
if (stuff.isEmpty())
return false;
int compareVal = order.compare(findMe, stuff.middle());
if (compareVal == 0)
return true;
else if (compareVal < 0)
return binarySearch(stuff.smaller(), findMe, compare);
else ; if (compareVal > 0)
return binarySearch(stuff.larger(), findMe, compare);
} // binarySearch(BinarySearchable, Object, Comparator)
D
/ \
B F
/ \ / \
A C E G
Rebelsky
/ \
Ferguson Walker
/ \ / \
Chamberland Herman Stone Wolf
/ / \
Adelberg Gum Jepsen
\ / \
Bishop 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.
Back to Implementing Dictionaries with Hash Tables. On to Discussion of Exam 3.
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[Honesty]
[Instructions]
[Links]
[Search]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]
Misc:
[Experiments in Java]
[Java API]
[Scheme Reference]
[Scheme Report]
[CS153 2003S]
[CS151 2003F]
[CS152 2000F]
[SamR]
Disclaimer:
I usually create these pages on the fly
, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Fri May 7 09:43:37 2004.
The source to the document was last modified on Tue Jan 13 10:26:12 2004.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2004S/Outlines/outline.53.html.
You may wish to
validate this document's HTML
;
;
Check with Bobby