Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Implement the find method of the below WeightedQuickUnionUF class such that

ID: 3862567 • Letter: J

Question

Java

Implement the find method of the below WeightedQuickUnionUF class such that it teturns the component id of site P(root of the tree that contains p). Also, considering the below initial parent array for N = 10 sites, complete the corresponding size array and the updated aprent and size arrays after the call to union (1, 8). public class WeightedQuickUnionUF {private int[] parent;//parent[i] = parent of i private int[] size;//size[i] = number of sites in subtree rooted at i public WeightedQuickUnionUF(int N) {parent = new int[N]; size = new int[N]; for (int i = 0; i

Explanation / Answer

public int find(int p) { validate(p); while (p != parent[p]) p = parent[p]; return p; } // validate that p is a valid index private void validate(int p) { int n = parent.length; if (p < 0 || p >= n) { throw new IndexOutOfBoundsException("index " + p + " is not between 0 and " + (n-1)); } }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote