Title: The Set ADT
1The Set ADT
Presented by Sam Fischbach 31 January 2001
2Overview of the Set ADT
The Set ADT is basically a more advanced version
of the Bag ADT, with a few differences and some
new methods.
Because of this, most abstract data types that
support Bag can support Set.
Lets look at what makes Set different, and which
ADTs can utilize the Set ADT.
3Properties of Set
Set has all of the properties of the Bag ADT
except that there can only ever be one instance
of an element.
Example 1,2,5,7 can be a set, but 1,2,2,2,5
is just a bag of numbers.
Because of this, Bags addElement method has to
check if there is already an element in the Set
that is the same as the one we want to add - so
you have to be able to compare the elements
somehow.
4Methods of Set
Besides the standard methods of Bag,
containsElement(Object) findElement(Object) remove
Element(Object)
Set supports the same basic operations that can
be performed on a mathematical set - the goal is
to make them identical.
The first operation is...
5Union
The unionWith(Set) method returns the union of
the calling set and a second set passed as the
argument. The output is then, of course, all
elements of Set A and Set B.
6Intersection
intersectWith(Set) is just like the mathematical
set operation as well. It will return the
elements shared by Set A and Set B.
The golden overlap between the two sets is the
result of the intersection.
7Complement
The differenceWith(Set) method corresponds with
the set operation complement. Set A calling
differenceWith(Set B) would return all of the
elements that are in A, less the same elements in
B.
8Subsets
9ADTs That Can Use Set
LinkedList, SortedVector, SkipList, AVLTree,
OpenHashtable, Hashtable - All of these support
the Bag ADT, and can therefore support Set.
Whether we would want to implement Set is
another matter
Consider execution time. A linked list can insert
an element at the beginning, which is O(1)
If we have to check every element for a
duplicate, as Set does, that jumps to O(n). Ouch!
10ADTs That Can Use Set ctd.
What about other ADTs though? The book points out
that some ADTs, like the AVLTree already have
O(log n) insertion times and the penalty for
using Set becomes negligible.
Of course, at some point we have to ask
ourselves, why are we using Set in the first
place?
11Example of Set In Use
Spellchecker
import java.io. import java.util.Enumeration im
port jds.Set import jds.collection.SortedSet imp
ort jds.util.StringCompare class SpellCheck
private static Set readwords(Reader in)
throws IOException Set result new
SortedSet(new StringCompare() )
StreamTokenizer tok new StreamTokenizer(in)
tok.ordinaryChar('.') //don't attach periods
to words tok.lowerCaseMode(true) int c
tok.nextToken() while( c !
StreamTokenizer.TT_EOF) if (c
StreamTokenizer.TT_WORD) result.addElement(
tok.aval) c tok.nextToken()
return result
12Example ctd.
public static void main(String args) try
// read the two sets of words Set
dict readWords(new FileReader("words"))
Set doc readWords(new FileReader("input"))
// remove the correctly spelled words
doc.differenceWith(dict) //
now print the misspellings Enumeration e
doc.elements() while(e.hasMoreElements())
System.out.println(e.nextElement())
catch(IOException e)
System.err.println("exception "e)
13Graphical Explanation of How it Works
Users Set
Dictionary Set
Cat Dog Aardvark Juraff
Cat Dog Aardvark Giraffe
Juraff
The difference of the users set and the
dictionary file leave only the words that were in
the users set but weren't in the dictionary.
14Summary
The Set ADT is a useful data type which holds
only unique elements and provides the basic
mathematical set operations. Other data types
can implement Set to increase their usefulness
and versatility.
Sets are cool.