Computer Science Fundamentals (CS153 2003S)
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[EC]
[Honesty]
[Instructions]
[Links]
[Search]
[Syllabus]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Lab Writeups]
[Outlines]
[Readings]
[Reference]
ECA:
[About]
[Grades]
[Quizzes]
[Submit Work]
[Change Password]
[Reset Password]
Misc:
[Experiments in Java]
[Scheme Reference]
[Scheme Report]
[CS153 2002S (Walker)]
[CS151 2003S (Rebelsky)]
[CS152 2000F (Rebelsky)]
[SamR]
Held: Monday, 5 May 2003
Summary: Today we consider a tree-based implementation of dictionaries, the binary search tree.
Related Pages:
Assignments:
Notes:
Overview:
middleelement.
smallerelements: everything less than the
middleelement.
largerelements: everything greater than the
middleelement.
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 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.
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[EC]
[Honesty]
[Instructions]
[Links]
[Search]
[Syllabus]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Lab Writeups]
[Outlines]
[Readings]
[Reference]
ECA:
[About]
[Grades]
[Quizzes]
[Submit Work]
[Change Password]
[Reset Password]
Misc:
[Experiments in Java]
[Scheme Reference]
[Scheme Report]
[CS153 2002S (Walker)]
[CS151 2003S (Rebelsky)]
[CS152 2000F (Rebelsky)]
[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 Tue May 6 09:21:13 2003.
The source to the document was last modified on Thu Jan 16 15:16:16 2003.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2003S/Outlines/outline.53.html.
You may wish to
validate this document's HTML
;
;
Check with Bobby