Complete the code to return the parent of element x in the Union Find structure.
int find(int parent[], int x) {
if (parent[x] == x) {
return x;
}
return [1];
}The find function recursively finds the root parent of x by calling find on parent[x].
Complete the code to implement path compression by updating parent[x] during find.
int find(int parent[], int x) {
if (parent[x] == x) {
return x;
}
parent[x] = [1];
return parent[x];
}Path compression updates parent[x] to the root found by recursively calling find on parent[x].
Fix the error in the union function to correctly merge two sets by their roots.
void union_sets(int parent[], int a, int b) {
a = find(parent, a);
b = find(parent, b);
if (a != b) {
parent[b] = [1];
}
}To merge sets, assign parent of root b to root a: parent[b] = a. Fill the blank with 'a'.
Fill both blanks to complete the union function that merges two sets by their roots.
void union_sets(int parent[], int a, int b) {
a = find(parent, a);
b = find(parent, b);
if (a != b) {
parent[[1]] = [2];
}
}To merge sets, assign parent of root b to root a: parent[b] = a.
Fill all three blanks to implement find with path compression and union that merges sets.
int find(int parent[], int x) {
if (parent[x] == x) {
return x;
}
parent[x] = [1];
return parent[x];
}
void union_sets(int parent[], int a, int b) {
a = [2];
b = [3];
if (a != b) {
parent[b] = a;
}
}In find, path compression assigns parent[x] to find(parent, parent[x]). In union, a and b are updated by find(parent, a) and find(parent, b) respectively.