(int node1, int node2)
| 34 | return parent[node]; |
| 35 | } |
| 36 | public boolean unionBySize(int node1, int node2){ |
| 37 | //1. find the root parent |
| 38 | int rootParent1 = findRootParent(node1); |
| 39 | int rootParent2 = findRootParent(node2); |
| 40 | if(rootParent1==rootParent2){ |
| 41 | return false; |
| 42 | } |
| 43 | // 2, union of components |
| 44 | if(size[rootParent1]<size[rootParent2]){ |
| 45 | parent[rootParent1] = rootParent2; |
| 46 | size[rootParent2] += size[rootParent1]; |
| 47 | }else { |
| 48 | parent[rootParent2] = rootParent1; |
| 49 | size[rootParent1] += size[rootParent2]; |
| 50 | } |
| 51 | return true; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 |
no test coverage detected