Algorithms and Data Structures 2005: Sets code

In the Algorithms and Data Structures course, the simple abstract data type Set is used, firstly to illustrate the concept of abstract data types, and then to illustrate how a great variety of data structures may be used to give different implementations of this one simple concept of a Set.

A set is defined as a collection of elements which has no concept of an element occurring a multiple number of times, and no concept of the elements having any order. So the only operations on a set are to add a new item, delete an existing item, and test whether a particular item is a member of the set. To keep things simple, we said there would be no operations that would result in "error" conditions. So deleting an item from a set in which that item is a not a member is a possible operation which just leaves the set unchanged, and adding an item to a set where it is already a member is a possible operation and leaves the set unchanged. Also to keep things simple we dealt only with sets of integers.

Set implementations

Here is a guide to the various implementations of sets:

"Front-end" or "driver" classes

The important thing in this course is the code which implements algorithms and data structures. However, in order that this code can be run and tested it is necessary to have a framework which makes use of it. Therefore classes have been introduced which provide such a "front-end". These classes need to interact with the human user and therefore involve use of some of Java's complicated input/output code. Please note that it is not required for this course that you study these front-end classes or understand how they work. Please, in fact, forget them, they are not important.

For sets, the front-end class in most cases provides a main method (when you run a Java program, the main method is where execution starts) which gives a very simple text-based program allowing the user to issue commands to manipulate a set. The program is not meant to be a good example of application design. It is designed to be as simple as possible in order that you should not be distracted by it. The idea is that there is a single set which is manipulated by commands typed in by the user. Initially the set is empty. The command p prints a representation of the set. The command a followed by a number on the same line adds that number to the set (if you use p again after that, you can see the change). The command d followed by a number deletes the number from the set. The command m followed by a number causes a message to be printed saying whether the number is in the set. For simplicity, adding a number to the set when it is already there, or deleting one which isn't there causes no change (rather than, for example, some sort of error condition). The command q causes the set manipulation program to halt.

Below are listed the various forms of the program used:


Matthew Huntbach
Last modified: 13 April 2005