I have just wrote a linked list implementation of disjoint sets in Java for the Rainfall Problem. Can you take a look and tell me if there is something missing and also make a judgement in performance?
public class DisJointSet<E>
{
int rank;
DNode<E> head;
DNode<E> tail;
public DisJointSet(E data)
{
DNode<E> node = new DNode<>(data);
head = node;
tail = node;
rank = 1;
node.represantive = this;
}
public void union(DNode<E> x, DNode<E> y)
{
DisJointSet<E> xSet = findSet(x);
DisJointSet<E> ySet = findSet(y);
if (xSet.rank>= ySet.rank)
append(xSet, ySet);
else
append(ySet, xSet);
}
public void append(DisJointSet<E> first, DisJointSet<E> second)
{
if (first.rank == 0 || second.rank == 0)
throw new IllegalArgumentException();
// Set the representative of each node in the second set to be
// the first set.
DNode<E> curr = second.head;
while(curr!=null)
{
curr.represantive = first;
curr = curr.next;
}
first.tail.next = second.head;
first.tail = second.tail;
first.rank += second.rank;
// Invalidate the second set, just to be sure.
second.head = null;
second.tail = null;
second.rank = 0;
}
public DisJointSet<E> findSet (DNode<E> x)
{
return x.represantive;
}
}
class DNode<E>
{
DNode<E> next;
DisJointSet<E> represantive;
E data;
DNode(E val)
{
data = val;
}
}