Java Reference
In-Depth Information
ȗ
void add(int x)
to add
x
if it is not present
ȗ
void remove(int x)
to remove
x
if it is present
ȗ
void print()
to print all elements currently in the set
ȗ
boolean find(int x)
to test whether
x
is present
Exercise P16.10. Reimplement the set class from Exercise P16.9 by using
a
TreeSet<Integer>
. In addition to the methods specified in Exercise
P16.9, supply an
iterator
method yielding an object that supports only
the
hasNext/next
methods.
The
next
method should return an
int
, not an object. For that reason,
you cannot simply return the iterator of the tree set.
Exercise P16.11. Reimplement the set class from Exercise P16.9 by using
a
TreeSet<Integer>
. In addition to the methods specified in Exercise
P16.9, supply methods
IntSet union(IntSet other)
IntSet intersection(IntSet other)
that compute the union and intersection of two sets.
Exercise P16.12. Implement the sieve of Eratosthenes: a method for
computing prime numbers, known to the ancient Greeks. Choose an n. This
method will compute all prime numbers up to n. First insert all numbers
from 2 to n into a set. Then erase all multiples of 2 (except 2); that is, 4, 6,
8, 10, 12, ș. Erase all multiples of 3; that is, 6, 9, 12, 15, ș. Go up to .
Then print the set.
n
Exercise P16.13. Write a method of the
BinarySearchTree
class
Comparable smallest()
that returns the smallest element of a tree. You will also need to add a
method to the
Node
class.