Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
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