Class SET<Key extends Comparable<Key>>
- Object
-
- edu.princeton.cs.algs4.SET<Key>
-
- Type Parameters:
Key
- the generic type of each key in this set
- All Implemented Interfaces:
Iterable<Key>
public class SET<Key extends Comparable<Key>> extends Object implements Iterable<Key>
TheSET
class represents an ordered set of comparable keys. It supports the usual add, contains, and delete methods. It also provides ordered methods for finding the minimum, maximum, floor, and ceiling and set methods for union, intersection, and equality.Even though this implementation include the method
equals()
, it does not support the methodhashCode()
because sets are mutable.This implementation uses a balanced binary search tree. It requires that the key type implements the
Comparable
interface and calls thecompareTo()
and method to compare two keys. It does not call eitherequals()
orhashCode()
. The add, contains, delete, minimum, maximum, ceiling, and floor methods take logarithmic time in the worst case. The size, and is-empty operations take constant time. Construction takes constant time.For additional documentation, see Section 3.5 of Algorithms in Java, 4th Edition by Robert Sedgewick and Kevin Wayne.
- Author:
- Robert Sedgewick, Kevin Wayne
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(Key key)
Adds the key to this set (if it is not already present).Key
ceiling(Key key)
Returns the smallest key in this set greater than or equal tokey
.boolean
contains(Key key)
Returns true if this set contains the given key.void
delete(Key key)
Removes the specified key from this set (if the set contains the specified key).boolean
equals(Object other)
Compares this set to the specified set.Key
floor(Key key)
Returns the largest key in this set less than or equal tokey
.int
hashCode()
This operation is not supported because sets are mutable.SET<Key>
intersects(SET<Key> that)
Returns the intersection of this set and that set.boolean
isEmpty()
Returns true if this set is empty.Iterator<Key>
iterator()
Returns all of the keys in this set, as an iterator.static void
main(String[] args)
Unit tests theSET
data type.Key
max()
Returns the largest key in this set.Key
min()
Returns the smallest key in this set.void
remove(Key key)
Removes the specified key from this set (if the set contains the specified key).int
size()
Returns the number of keys in this set.String
toString()
Returns a string representation of this set.SET<Key>
union(SET<Key> that)
Returns the union of this set and that set.-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Method Detail
-
add
public void add(Key key)
Adds the key to this set (if it is not already present).- Parameters:
key
- the key to add- Throws:
IllegalArgumentException
- ifkey
isnull
-
contains
public boolean contains(Key key)
Returns true if this set contains the given key.- Parameters:
key
- the key- Returns:
true
if this set containskey
;false
otherwise- Throws:
IllegalArgumentException
- ifkey
isnull
-
delete
public void delete(Key key)
Removes the specified key from this set (if the set contains the specified key). This is equivalent toremove()
, but we plan to deprecatedelete()
.- Parameters:
key
- the key- Throws:
IllegalArgumentException
- ifkey
isnull
-
remove
public void remove(Key key)
Removes the specified key from this set (if the set contains the specified key). This is equivalent todelete()
, but we plan to deprecatedelete()
.- Parameters:
key
- the key- Throws:
IllegalArgumentException
- ifkey
isnull
-
size
public int size()
Returns the number of keys in this set.- Returns:
- the number of keys in this set
-
isEmpty
public boolean isEmpty()
Returns true if this set is empty.- Returns:
true
if this set is empty;false
otherwise
-
iterator
public Iterator<Key> iterator()
Returns all of the keys in this set, as an iterator. To iterate over all of the keys in a set namedset
, use the foreach notation:for (Key key : set)
.- Specified by:
iterator
in interfaceIterable<Key extends Comparable<Key>>
- Returns:
- an iterator to all of the keys in this set
-
max
public Key max()
Returns the largest key in this set.- Returns:
- the largest key in this set
- Throws:
NoSuchElementException
- if this set is empty
-
min
public Key min()
Returns the smallest key in this set.- Returns:
- the smallest key in this set
- Throws:
NoSuchElementException
- if this set is empty
-
ceiling
public Key ceiling(Key key)
Returns the smallest key in this set greater than or equal tokey
.- Parameters:
key
- the key- Returns:
- the smallest key in this set greater than or equal to
key
- Throws:
IllegalArgumentException
- ifkey
isnull
NoSuchElementException
- if there is no such key
-
floor
public Key floor(Key key)
Returns the largest key in this set less than or equal tokey
.- Parameters:
key
- the key- Returns:
- the largest key in this set table less than or equal to
key
- Throws:
IllegalArgumentException
- ifkey
isnull
NoSuchElementException
- if there is no such key
-
union
public SET<Key> union(SET<Key> that)
Returns the union of this set and that set.- Parameters:
that
- the other set- Returns:
- the union of this set and that set
- Throws:
IllegalArgumentException
- ifthat
isnull
-
intersects
public SET<Key> intersects(SET<Key> that)
Returns the intersection of this set and that set.- Parameters:
that
- the other set- Returns:
- the intersection of this set and that set
- Throws:
IllegalArgumentException
- ifthat
isnull
-
equals
public boolean equals(Object other)
Compares this set to the specified set.Note that this method declares two empty sets to be equal even if they are parameterized by different generic types. This is consistent with the behavior of
equals()
within Java's Collections framework.
-
hashCode
public int hashCode()
This operation is not supported because sets are mutable.- Overrides:
hashCode
in classObject
- Returns:
- does not return a value
- Throws:
UnsupportedOperationException
- if called
-
toString
public String toString()
Returns a string representation of this set.
-
main
public static void main(String[] args)
Unit tests theSET
data type.- Parameters:
args
- the command-line arguments
-
-