Question: I need help modifiying this code to meet the requirements in the pictures. This is the code: / / Java code that implements disjontsets with
I need help modifiying this code to meet the requirements in the pictures. This is the code:
Java code that implements disjontsets with Union by height and path compression
import java.util.;
public class DisjiontSetPathCompress
A little driver program to test our class.
public static void mainString args
Scanner stdin new ScannerSystemin;
System.out.printlnHow many items do you want in your Disjoint Set?";
int n stdin.nextInt;
Main mySet new Mainn; make the sets Keep on going till the user wants to quit.
while true
System.out.printlnDo you want to quityes, no;
int ans stdin.nextInt;
if ans break;
Get the two items to union.
System.out.printlnWhich two items do you want to bring together, through n;
int item stdin.nextInt;
int item stdin.nextInt;
See if it worked!
boolean result mySet.unionitem item;
if result
System.out.printlnSorry those were already together!";
else
System.out.printlnThe union was successful, here is the new parent list: mySet;
private pair parents;
Create the initial state of a disjoint set of n elements, to n
public Mainint n
All nodes start as leaf nodes.
parents new pairn;
for int i; i parentsrootgetHeight
parentsrootsetIDroot;
Attach tree to tree
else if parentsrootgetHeight parentsrootgetHeight
parentsrootsetIDroot;
Same height case just attach tree to tree adjust height.
else
parentsrootsetIDroot;
parentsrootincHeight;
We successfully did a union.
return true;
Just represents this object as a list of each node's parent.
public String toString
String ans ;
for int i; i
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
