Package dsa
Class WeightedQuickUnionUF
java.lang.Object
dsa.WeightedQuickUnionUF
- All Implemented Interfaces:
UF
This data type provides an implementation of the Union Find (UF) API in which the count operation runs in constant
time, whereas the find, connected, and union operations run in logarithmic time.
-
Constructor Summary
ConstructorDescriptionWeightedQuickUnionUF
(int n) Constructs an empty UF data structure with n sites. -
Method Summary
Modifier and TypeMethodDescriptionboolean
connected
(int p, int q) Returnstrue
if sitesp
andq
belong to the same component, andfalse
otherwise.int
count()
Returns the number of components.int
find
(int p) Returns the canonical site of the component containing sitep
.static void
Unit tests the data type.void
union
(int p, int q) Connects sitesp
andq
.
-
Constructor Details
-
WeightedQuickUnionUF
public WeightedQuickUnionUF(int n) Constructs an empty UF data structure with n sites.- Parameters:
n
- the number of sites.
-
-
Method Details
-
find
public int find(int p) Returns the canonical site of the component containing sitep
. -
count
public int count()Returns the number of components. -
connected
public boolean connected(int p, int q) Returnstrue
if sitesp
andq
belong to the same component, andfalse
otherwise. -
union
public void union(int p, int q) Connects sitesp
andq
. -
main
Unit tests the data type.- Parameters:
args
- the command-line arguments.
-