MCPcopy Create free account
hub / github.com/ByteByteGoHq/coding-interview-patterns / union

Method union

java/Graphs/ConnectTheDots.java:19–36  ·  view source on GitHub ↗
(int x, int y)

Source from the content-addressed store, hash-verified

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]) {

Callers 1

connectTheDotsMethod · 0.95

Calls 1

findMethod · 0.95

Tested by

no test coverage detected