Answered step by step
Verified Expert Solution
Link Copied!

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 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
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Securing SQL Server Protecting Your Database From Attackers

Authors: Denny Cherry

3rd Edition

0128012757, 978-0128012758

More Books

Students also viewed these Databases questions

Question

* What is the importance of soil testing in civil engineering?

Answered: 1 week ago

Question

Explain the concept of shear force and bending moment in beams.

Answered: 1 week ago