T
- the type of elements maintained by this setpublic class DisjointSetForest<T> extends Object implements Set<T>
A disjoint set holds a set of elements partitioned into a number of non-overlapping subsets. The disjoint set forest holds each subset as a tree structure, with the representative of each subset being the root of the respective tree.
See http://en.wikipedia.org/wiki/Disjoint-set_data_structure for more information.
Constructor and Description |
---|
DisjointSetForest()
Construct a new Disjoint Set Forest.
|
DisjointSetForest(int initialCapacity)
Constructs a new Disjoint Set Forest, with the specified initial
capacity.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(T e) |
T |
add(T elem,
T repr)
Add an element to the given set.
|
boolean |
addAll(Collection<? extends T> collection)
Add all the elements in the collection to this disjoint set forest.
|
boolean |
addAll(Collection<? extends T> collection,
boolean sameSet)
Add all the elements in the collection to this disjoint set forest.
|
List<T> |
asList() |
void |
clear() |
boolean |
contains(Object o) |
boolean |
containsAll(Collection<?> collection) |
T |
find(T x)
Search for the representative of the subset containing the element x.
|
Set<Set<T>> |
getSubsets()
Get all the subsets stored in this forest.
|
boolean |
isEmpty() |
Iterator<T> |
iterator() |
T |
makeSet(T o)
Make a new subset from the given item
|
int |
numSets()
Get the number of subsets
|
static <T> DisjointSetForest<T> |
partition(List<T> values,
Comparator<T> comparator)
Construct a
DisjointSetForest by partitioning the given values
using the given Comparator to determine equality of pairs of
values. |
static <T> Set<Set<T>> |
partitionSubsets(List<T> values,
Comparator<T> comparator)
Extract the subsets formed by constructing a
DisjointSetForest
which partitions the given values using the given Comparator to
determine equality of pairs of values. |
boolean |
remove(Object o) |
boolean |
removeAll(Collection<?> c) |
boolean |
retainAll(Collection<?> c) |
int |
size() |
int |
size(T o)
Get the size of a subset
|
Object[] |
toArray() |
<E> E[] |
toArray(E[] a) |
T |
union(T x,
T y)
Joint the subsets belonging to the objects x and y.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
equals, hashCode, spliterator
parallelStream, removeIf, stream
public DisjointSetForest()
public DisjointSetForest(int initialCapacity)
initialCapacity
- The initial capacitypublic T find(T x)
x
- the elementpublic T makeSet(T o)
o
- the itempublic T union(T x, T y)
x
- the x object.y
- the y object.public int size()
public boolean isEmpty()
public <E> E[] toArray(E[] a)
public T add(T elem, T repr)
elem
- Element to addrepr
- A representative of the set to add toIllegalArgumentException
- If repr is not an element of this forest.public boolean containsAll(Collection<?> collection)
containsAll
in interface Collection<T>
containsAll
in interface Set<T>
public boolean addAll(Collection<? extends T> collection)
public boolean addAll(Collection<? extends T> collection, boolean sameSet)
collection
- The collection to addsameSet
- Are the elements in the collection to be added to the same
set?public boolean retainAll(Collection<?> c)
public boolean removeAll(Collection<?> c)
public void clear()
public int size(T o)
o
- an object in the subsetpublic int numSets()
public Set<Set<T>> getSubsets()
public static <T> DisjointSetForest<T> partition(List<T> values, Comparator<T> comparator)
DisjointSetForest
by partitioning the given values
using the given Comparator
to determine equality of pairs of
values.T
- the type of elements maintained by this setvalues
- the values to partitioncomparator
- the comparator to determine equality of valuesDisjointSetForest
representing the partitioningpublic static <T> Set<Set<T>> partitionSubsets(List<T> values, Comparator<T> comparator)
DisjointSetForest
which partitions the given values using the given Comparator
to
determine equality of pairs of values.T
- the type of elements maintained by this setvalues
- the values to partitioncomparator
- the comparator to determine equality of values