You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
publicclassQuickUnionPathComparisonUF
{
privateint[] id;
// set id of each object to itself (N array accesses)publicQuickUnionUF(intN)
{
id = newint[N];
for (inti = 0; i < N; i++) id[i] = i;
}
// chase parent pointers until reach root (depth of i array accesses)privateintroot(inti)
{
while (i != id[i])
{
id[i] = id[id[i]]; // only change with quick unioni = id[i];
}
returni;
}
// check if p and q have same root (depth of p and q array accesses)publicbooleanconnected(intp, intq)
{
returnroot(p) == root(q);
}
// change root of p to point to root of q (depth of p and q array accesses)publicvoidunion(intp, intq)
{
inti = root(p);
intj = root(q);
id[i] = j;
}
}