Question
Give the id[] array and draw the corresponding forest-of-trees representation that results from the following sequence of union operations using the quick-union algorithm described starting
Give the id[] array and draw the corresponding forest-of-trees representation that results from the following sequence of union operations using the quick-union algorithm described starting on p. 224:
0-2, 1-4, 2-5, 3-6, 0-4, 0-6, 1-3
Assume that N equals 7. Warning: Make sure that your id[] array is produced exactly as in the code on p. 224do not interchange the roles of p and q.
Code in question on page 224:
private int find(int p)
{
while (p != id[p]) p = id[p];
return p;
}
public void union(int p, int q)
{
int pRoot = find(p);
int qRoot = find(q);
if (pRoot == qRoot) return;
id[pRoot] = qRoot;
count--;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started