Design and implement Java classes for representing and
manipulating
generic disjoint sets. You will use a tree set representation, similar
to the one described in the textbook. A set consists of a Set
class instance, and zero or more Element
class instances,
one for each element that is a member of the set. The Element
instances are arranged as a tree, the root of which is the set
representative Element
instance (i.e., the Element
instance that corresponds to the set representative element). Each Element
instance that is not a set representative keeps a reference to its
parent Element
instance in the tree. A
representative Element
instance keeps a reference to the Set
instance of the
set, instead. A Set
instance contains set
size, and a
reference to the set representative Element
instance.
Your
set implementation should support efficient FindSet
and Union
operations. The FindSet
operation should
provide the
option of using the path compression
heuristic. The Union
operation should implement union by size.
For more details, including exact specifications of the
methods that
you should implement, see the starter code. All of your work must be
done inside the Element
and Set
classes,
where indicated by comments that start with: "// TODO:
".
You may also add appropriate instance variables or helper methods. The
tester that we provide below is very
basic, running it successfully does not guarantee that your code is
correct or will pass our marking tester routine.
You are encouraged to derive your own test cases.
Submission Instructions:
Examples:
Sample representation of two sets:Union(s1, s2)
.
The call
returns a reference to s2
.FindSet(e2)
with path
compression, after we execute the above union operation. The call
returns a reference to s2
.