MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / unionBySize

Method unionBySize

RedundantConnection.java:36–52  ·  view source on GitHub ↗
(int node1, int node2)

Source from the content-addressed store, hash-verified

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

Callers 1

Calls 1

findRootParentMethod · 0.95

Tested by

no test coverage detected