next up previous

CSE 2320 Section 060/061 Spring 1999

Program 3

Due: April 1, 1999, 1pm (April 2, 1999, 5:00pm for -10%)

In order to support faster search times this program will implement the same set operations as PGM2, but using binary search trees as the underlying data structure for each set. Of course, we know that in the worst case, the binary search tree will be linear, and search will have the same complexity as in our linked-list solution from PGM2. Therefore, we are going to take advantage of Theorem 13.6 on page 258 and randomly shuffle the set elements before insertion so that we obtain $O(\lg n)$search times on average. As usual, you cannot use global variables.

Define data structures for the binary search tree (BST). Implement procedures for tree allocation, deallocation, insertion, deletion, search and INORDER printing. All references to ``set'' below imply the use of a BST to represent the set. As before, sets will contain only positive integers.

Implement the set operations union(S1,S2), intersection(S1,S2) and difference(S1,S2). Each of these operations creates and returns a new set which is the result of the appropriate set operation. Union returns the set of unique elements in either S1 or S2. Intersection returns the set of unique elements in both S1 and S2. Difference returns the set of elements in S1 that are not in S2.

Each time you create a set, either directly or as the result of a set operation, you should first collect the elements of the set in an array and then randomly shuffle the array. Specifically, if the array contains n elements, then perform n swaps within the array, each time choosing two random integers between 0 and n-1 as the indices of the elements to swap. See the file random.c for an example of how to do this.

Your main program will read commands from a file whose name is given as the first argument to your executable. The syntax of the possible commands are shown in the following table. Set identifiers are of the form Sn, where n is a single digit (0-9). You may assume these ten sets are initially empty. Symbols within commands are separated by a single space.

Command Explanation
assign Sn = { $a_1 \; a_2 \; \ldots \; a_m$ } Create a new set containing the positive integers $a_1 \ldots a_m$ and assign it to the set Sn. You may assume the integers are unique and less than 1000.
assign Sn = Sn U Sn Create a new set that is the union of the second and third set and assign it to the first set.
assign Sn = Sn & Sn Create a new set that is the intersection of the second and third set and assign it to the first set.
assign Sn = Sn - Sn Create a new set that is the difference between the second and third set and assign it to the first set.
print Sn Print the elements of the set in order in the same format as the first set command above.

In the assign commands, if the first set Sn is already defined, this existing set should first be deallocated. All dynamically allocated memory should be explicitly deallocated before your program exits.

Your main program should output each command read in with a preceding `> '. Your program should also print ``okay'' on a separate line after each assign command. Your program should print the elements of the set after each print command. See the files pgm3in and pgm3out in the class directory on omega for an example. These are identical to pgm2in and pgm2out.

Be sure to properly document your source code, as described in Program 1. After ensuring that your program compiles and executes correctly on the omega machine, run the program /public/cse/2320-061/handin3 to electronically hand in your source file. All source code must be in one file (i.e., no separate .h and .c files).

next up previous