- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnionFind.java
More file actions
Latest commit
40 lines (33 loc) · 907 Bytes
/
Copy pathUnionFind.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
publicclassUnionFind {
publicLLAddOnlymakeSet(Cellc) {
LLAddOnlyset = newLLAddOnly();
set.add(c);
returnset;
}
publicLLAddOnlyfind(Cellc) {
LLAddOnlytoReturn = c.head;
returntoReturn;
}
// adds two LLAddOnly's together
publicvoidunion(Cellone, Celltwo) {
// if first cell list is larger add second to first
if(one.head.size>=two.head.size) {
CelltwoFirst = 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 {
CelloneFirst = one.head.first;
// adds the second cell's list to the first cells list
unionize(two,oneFirst);
}
}
// bigger cell is cell one
privatevoidunionize(Cellone, Celltwo) {
if(two.next!=null) {
unionize(one, two.next);
}
one.head.add(two);
}
}