CS 222 Fall 2005,
Instructor: Jeffrey Horn
HOMEWORK 3 (program 2) BINARY SEARCH
DESCRIPTION
Package up the binary search code we developed in class.
Include both the iterative and the recursive versions. Implement the
"list" ADT in a class with member methods/functions for the ADT operations
"insert", "delete", and "member". Write test code in your "main" to create
an object of your class and test it. Use a large (10000 elements) file of
input to test your data. Write a small piece of code to generate the file
of random values. Choose any one of the following data types for
searching: int, float, double, string. Sorting? Yes you
need a sorted array for binary search to work, but don't bother to write a sort
method. Instead, just make sure that your "insert" method inserts into the
array in sorted order. In other words, it finds where to insert the new
element (you can use your binary search code to do this quickly!) and then shift
right a portion of the array to allow the insert. Your code can
allow duplicates (essentially by ignoring them) or it can detect them before
insertion and simply not allow them. You may choose how to handle
duplicates. But you must say in your comments how you handle them.
Feel free to generate console messages warning about duplicates, but do not stop
executing just because of that!
SPECIFICS
- Data Structure: Use two arrays, say "A" and
"B". "A" has the elements in sorted order, while "B" will be
used to store the "binary search tree" in an array.
- Methods:
- empty() Boolean. Returns true if
array is empty, false otherwise.
- insert(x): Insert the value "x"
into the array, keeping the array in sorted order. If the array is
full, you can enlarge the array (e.g., by declaring a new array of
larger size and copying the values into the new array), or generate an
error message (e.g., to the console, and also return "false").
That is up to you. Just make sure you can handle a pretty big test
case (e.g., 10000 elements).
- delete(x): Delete the value "x" if it is
in the array. If multiple copies of "x", you can handle that by
just deleting any one of them.
- bool member(x): This is where the binary
search goes! Note that you might also want a private "member(x)"
method that returns the location (index) of "x", rather than just
boolean (i.e., whether or not "x" is found in the array).
- bool r_member(x): Do the same as the
above but use a recursive method to implement the binary search.
See our textbook for the pseudocode for recursive binary search.
The actual recursive method (i.e., the method that actually calls
itself) could be a private method. In other words, your "bool
r_member(x)" method could simply call the recursive method once.
Or "bool r_member(x)" could be the recursive function. Whatever is
easier for you. Just implement recursive binary search!
- Lastly, you must use the other array (I'll call it "B" here) to
implement the "binary search tree in an array". I can think of two
ways to do this:
- One method (bool tree_member(x)), which, when called, copies the
values in array A into B (but in breadth-first order), and then does
a binary search using the tree structure (i.e., it starts at the
root and then continues down the left or right subtrees). You
can use either the interative or recursive form of binary search.
With this approach you do not have to implement a separate insert or
delete method. This way you do not have to keep B updated.
- Replicate all methods (insert, delete, member) with versions
that use array B. This means you have to keep B updated.
EXTRA CREDIT FOR THIS! ( don't know how much yet...)