public final class UnionFind<T>
extends java.lang.Object
Constructor and Description |
---|
UnionFind()
Creates a new UnionFind structure that is initially empty.
|
UnionFind(java.util.Collection<? extends T> elems)
Creates a new UnionFind structure that initially contains all of
the elements from the specified container.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(T elem)
Inserts a new element into the UnionFind structure that initially
is in its own partition.
|
T |
find(T elem)
Given an element, returns the representative element of the set
containing that element.
|
void |
union(T one,
T two)
Given two elements, unions together the sets containing those
elements.
|
public UnionFind()
public UnionFind(java.util.Collection<? extends T> elems)
elems
- The elements to store in the UnionFind structure.public boolean add(T elem)
elem
- The element to insert.java.lang.NullPointerException
- If elem is null.public T find(T elem)
elem
- The element to look up.java.util.NoSuchElementException
- If the element does not exist.public void union(T one, T two)
one
- The first element to link.two
- The second element to link.java.util.NoSuchElementException
- If either element does not exist.