Question: Covert code from java to c++ , Please don't switch how the code works, just need a simple conversion. public class QuickUnionUF { private int

Covert code from java to c++ , Please don't switch how the code works, just need a simple conversion.

public class QuickUnionUF {

private int[] parent; // parent[i] = parent of i

private int count; // number of components

/*

* Initializes an empty union-find data structure with

* {@code n} elements {@code 0} through {@code n-1}.

* Initially, each elements is in its own set.

*

* @param n the number of elements

* @throws IllegalArgumentException if {@code n < 0}

*/

public QuickUnionUF(int n) {

parent = new int[n];

count = n;

for (int i = 0; i < n; i++) {

parent[i] = i;

}

}

/*

* Returns the number of sets.

*

* @return the number of sets (between {@code 1} and {@code n})

*/

public int count() {

return count;

}

/*

* Returns the canonical element of the set containing element {@code p}.

*

* @param p an element

* @return the canonical element of the set containing {@code p}

* @throws IllegalArgumentException unless {@code 0 <= p < n}

*/

public int find(int p) {

validate(p);

while (p != parent[p])

p = parent[p];

return p;

}

// validate that p is a valid index

private void validate(int p) {

int n = parent.length;

if (p < 0 || p >= n) {

throw new IllegalArgumentException("index " + p + " is not between 0 and " + (n-1));

}

}

/*

* Returns true if the two elements are in the same set.

*

* @param p one element

* @param q the other element

* @return {@code true} if {@code p} and {@code q} are in the same set;

* {@code false} otherwise

* @throws IllegalArgumentException unless

* both {@code 0 <= p < n} and {@code 0 <= q < n}

* @deprecated Replace with two calls to {@link #find(int)}.

*/

@Deprecated

public boolean connected(int p, int q) {

return find(p) == find(q);

}

/*

* Merges the set containing element {@code p} with the

* the set containing element {@code q}.

*

* @param p one element

* @param q the other element

* @throws IllegalArgumentException unless

* both {@code 0 <= p < n} and {@code 0 <= q < n}

*/

public void union(int p, int q) {

int rootP = find(p);

int rootQ = find(q);

if (rootP == rootQ) return;

parent[rootP] = rootQ;

count--;

}

public void displaySubsets(){

System.out.print("[");

for (int i = 0; i < parent.length; i++)

System.out.print(parent[i]+" ");

System.out.println("] ");

}

/*

* replacement main( ) for QuickUnionUF.java

* Given a set of n = 64 objects* Test QuickUnionUFin three different ways and print the results

* Test 2(a): u(0, 1), u(1, 2), u(2, 3) ... u(62, 63)

* Test 2(b): u(0, 63), u(1, 63), u(2, 63) ... u(62, 63)

* Test 3(c): Merge into 16 subsets each of size 4, then 4 subsets each of size 16,

* then one subset of all 64 element

*/

public static void main(String[] args) {

int n = 64;

QuickUnionUF uf= new QuickUnionUF(n);

for ( int k = 0; k < n-1; k++ ) uf.union(k, k+1);

uf.displaySubsets( ); // Test 2(a)

uf= new QuickUnionUF(n); // equivalent to reset...

for ( int k = 0; k < n-1; k++ ) uf.union(k, n-1);

uf.displaySubsets( );// Test 2(b)

uf= new QuickUnionUF(n);// equivalent to reset...

for ( int k = 0; k < n-1; k += 4 )

{ uf.union(k, k+1); uf.union(k+2, k+3); uf.union(k, k+3); }

for ( int k = 0; k < n-1; k += 16 )

{ uf.union(k, k+4); uf.union(k, k+8); uf.union(k, k+12); }

uf.union(0, 16); uf.union(0, 32); uf.union(0, 48);

uf.displaySubsets( ); // Test 2(c)}

}

}

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!