T
- element typepublic class UnionFind<T> extends Object
Constructor and Description |
---|
UnionFind(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 Map<T,T> |
getParentMap() |
protected Map<T,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.
|
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 void addElement(T element)
element
- The element to add.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()
Copyright © 2019. All rights reserved.