Set-theoretical Operations

  • Describe set-theoretical operations union, intersection, and set difference.

In Mathematics, two sets can be "added" together. Thus, for example, the union of AA and BB, denoted by ABA \cup B, is the set of all elements of either AA or BB.

We can include this operation in the Set ADT:

/** * Constructing a new set with elements that are in this set or * in the other set. * * @param other set. * @return all elements that are in this set or the other set. */ Set<T> union(Set<T> other);

The intersection of AA and BB, denoted by ABA \cap B, is the set of elements that are members of both AA and BB.

/** * Constructing a new set with elements that are in this set and * in the other set. * * @param other set. * @return the elements this set and other set have in common. */ Set<T> intersect(Set<T> other);

The set difference of AA and BB, denoted by ABA - B, is the set of all elements that are in AA but not in BB.

/** * Constructing a new set with elements that are in this set but not * in the other set. * * @param other set. * @return the elements in this set but not in the other set. */ Set<T> subtract(Set<T> other);

These operations can be defined for OrderedSet ADT as well:

OrderedSet<T> union(OrderedSet<T> other); OrderedSet<T> intersect(OrderedSet<T> other); OrderedSet<T> subtract(OrderedSet<T> other);

We leave it to you as a challenge exercise to implement these operations efficiently.