T - element typepublic class UnionFind<T>
extends java.lang.Object
| Constructor and Description |
|---|
UnionFind(java.util.Set<T> elements)
Creates a UnionFind instance with all the elements in separate sets.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addElement(T element)
Adds a new element to the data structure in its own set.
|
T |
find(T element)
Returns the representative element of the set that element is in.
|
protected java.util.Map<T,T> |
getParentMap() |
protected java.util.Map<T,java.lang.Integer> |
getRankMap() |
boolean |
inSameSet(T element1,
T element2)
Tests whether two elements are contained in the same set.
|
int |
numberOfSets()
Returns the number of sets.
|
void |
reset()
Resets the UnionFind data structure: each element is placed in its own singleton set.
|
int |
size()
Returns the total number of elements in this data structure.
|
java.lang.String |
toString()
Returns a string representation of this data structure.
|
void |
union(T element1,
T element2)
Merges the sets which contain element1 and element2.
|
public UnionFind(java.util.Set<T> elements)
elements - the initial elements to include (each element in a singleton set).public void addElement(T element)
element - The element to add.protected java.util.Map<T,T> getParentMap()
protected java.util.Map<T,java.lang.Integer> getRankMap()
public T find(T element)
element - The element to find.public void union(T element1, T element2)
element1 - The first element to union.element2 - The second element to union.public boolean inSameSet(T element1, T element2)
element1 - first elementelement2 - second elementpublic int numberOfSets()
public int size()
public void reset()
public java.lang.String toString()
toString in class java.lang.ObjectCopyright © 2019. All Rights Reserved.