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 main(String[] args){
Scanner stdin = new Scanner(System.in);
System.out.println("How many items do you want in your Disjoint Set?");
int n = stdin.nextInt();
Main mySet = new Main(n); //make the sets// Keep on going till the user wants to quit.
while (true){
System.out.println("Do you want to quit(1=yes, 0=no)?");
int ans = stdin.nextInt();
if (ans ==1) break;
// Get the two items to union.
System.out.println("Which two items do you want to bring together, 0 through "+(n-1)+"?");
int item1= stdin.nextInt();
int item2= stdin.nextInt();
// See if it worked!
boolean result = mySet.union(item1, item2);
if (!result){
System.out.println("Sorry, those were already together!");
}
else {
System.out.println("The union was successful, here is the new parent list: "+mySet);
}
}
}private pair[] parents;
// Create the initial state of a disjoint set of n elements, 0 to n-1.
public Main(int n){
// All nodes start as leaf nodes.
parents = new pair[n];
for (int i=0; i parents[root2].getHeight()){
parents[root2].setID(root1);
}// Attach tree 1 to tree 2
else if (parents[root2].getHeight()> parents[root1].getHeight()){
parents[root1].setID(root2);
}// Same height case - just attach tree 2 to tree 1, adjust height.
else {
parents[root2].setID(root1);
parents[root1].incHeight();
}// 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=0; i
 I need help modifiying this code to meet the requirements in

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!