-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDisjointSet.java
59 lines (41 loc) · 1.45 KB
/
DisjointSet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
public class DisjointSet {
private int[] array;
public DisjointSet(int arraySize) {
setArray(new int[arraySize]);
for (int i = 0; i < getArray().length; i++) {
getArray()[i] = -1;
}
}
public int find(int x) {
if (getArray()[x] < 0) {
return x; // x is the root of the tree; return it
} else {
// Find out who the root is; compress path by making the root x's parent.
getArray()[x] = find(getArray()[x]);
return getArray()[x]; // Return the root
}
}
public void union(int root1, int root2) {
if (getArray()[root2] < getArray()[root1]) { // root2 has larger tree
getArray()[root2] += getArray()[root1]; // update # of items in root2's tree
getArray()[root1] = root2; // make root2 new root
} else { // root1 has equal or larger tree
getArray()[root1] += getArray()[root2]; // update # of items in root1's tree
getArray()[root2] = root1; // make root1 new root
}
}
@Override
public String toString() {
String string = "";
for(int i : getArray()) {
string += i + " ";
}
return string;
}
public int[] getArray() {
return array;
}
public void setArray(int[] array) {
this.array = array;
}
}