Needs Implemented Java Disjoint Sets Data Structure Implemented Trees Problem Universal Se Q43834364
Needs to be implemented in Java
A disjoint sets data structure can be implemented by trees. Inthis problem, the universal set is equal to U = {0, 1, . . . , N −1}, and trees are stored in an array id of parents: the parent ofelement i is equal to id(i); if id(i) = i, this means that i is inthe root of the tree and is thus a representative of the set.
You need to implement disjoint sets data structure (that is,Union-Find data structure). Follow the following instructions:
– In class UnionFind add a constructur UnionFind(int N), whichmakes N oneelement sets, that is, id(i) = i for each index i.
– In class UnionFind implement the following methods:
(i) find, which takes an integer i and returns therepresentative of the set which contains the element i. You alsoneed to consider path compression, that is, the method makes thedirect reference from each element traversed during the search tothe root. 3
(ii) unite, which takes two integers p and q and makes the unionbetween the set containing the element p and the set containing theelement q. As for the representative of the new set it takes p.
(iii) isInSameSet, which takes two integer p and q and returnstrue, if the elements p and q are in the same set, and falseotherwise.
Expert Answer
Answer to Needs to be implemented in Java A disjoint sets data structure can be implemented by trees. In this problem, the univers…
OR