-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnionFind.java
More file actions
40 lines (33 loc) · 907 Bytes
/
UnionFind.java
File metadata and controls
40 lines (33 loc) · 907 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public class UnionFind {
public LLAddOnly makeSet(Cell c) {
LLAddOnly set = new LLAddOnly();
set.add(c);
return set;
}
public LLAddOnly find(Cell c) {
LLAddOnly toReturn = c.head;
return toReturn;
}
// adds two LLAddOnly's together
public void union(Cell one, Cell two) {
// if first cell list is larger add second to first
if(one.head.size>=two.head.size) {
Cell twoFirst = two.head.first;
// adds the second cell's list to the first cells list
unionize(one,twoFirst);
}
// if second cell list is larger than first. add first to second
else {
Cell oneFirst = one.head.first;
// adds the second cell's list to the first cells list
unionize(two,oneFirst);
}
}
// bigger cell is cell one
private void unionize(Cell one, Cell two) {
if(two.next!=null) {
unionize(one, two.next);
}
one.head.add(two);
}
}