(UnionFind<X> y)
| 54 | } |
| 55 | |
| 56 | public void union(UnionFind<X> y) |
| 57 | { |
| 58 | UnionFind<X> xRoot = this.find(); |
| 59 | UnionFind<X> yRoot = y.find(); |
| 60 | if (xRoot == yRoot) |
| 61 | return; // x and y are not already in same set. Merge them. |
| 62 | |
| 63 | if (xRoot.rank < yRoot.rank) |
| 64 | xRoot.parent = yRoot; |
| 65 | else if (xRoot.rank > yRoot.rank) |
| 66 | yRoot.parent = xRoot; |
| 67 | else |
| 68 | { |
| 69 | yRoot.parent = xRoot; |
| 70 | xRoot.rank = xRoot.rank + 1; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | } |