| 17 | } |
| 18 | |
| 19 | public boolean union(int x, int y) { |
| 20 | int repX = find(x); |
| 21 | int repY = find(y); |
| 22 | if (repX != repY) { |
| 23 | if (this.size[repX] > this.size[repY]) { |
| 24 | this.parent[repY] = repX; |
| 25 | this.size[repX] += this.size[repY]; |
| 26 | } |
| 27 | else { |
| 28 | this.parent[repX] = repY; |
| 29 | this.size[repY] += this.size[repX]; |
| 30 | } |
| 31 | // Return True if both groups were merged. |
| 32 | return true; |
| 33 | } |
| 34 | // Return False if the points belong to the same group. |
| 35 | return false; |
| 36 | } |
| 37 | |
| 38 | public int find(int x) { |
| 39 | if (x == this.parent[x]) { |