-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnionFind.java
55 lines (39 loc) · 1.13 KB
/
UnionFind.java
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package Structure;
import java.util.ArrayList;
public class UnionFind {
// Variable d'instance
ArrayList<Ensemble> parent = new ArrayList();
Ensemble ensemble ;
public void makeSet(int x) {
boolean exist=false;
for (Ensemble e : parent) {
if (e.representant==x)
exist = true;
}
if (!exist) {
ensemble = new Ensemble(x);
parent.add(ensemble);
//System.out.println(ensemble.representant);
}
}
public Ensemble Find(int x) {
for (Ensemble ensemble : parent ) {
if (ensemble.representant != null && ensemble.representant==x)
return ensemble ;
}
return null;
}
public void Union(int x, int y) {
Ensemble es1 = this.Find(x);
Ensemble es2 = this.Find(y);
if (es1 != null && es2 != null) {
if (es1.representant != es2.representant)
es1.getListeChaineeS().concatener(es2.getListeChaineeS());
//parent.remove(es2);
}
}
public void afficheElement() {
for (Ensemble ensemble : parent)
System.out.println(ensemble.toString());
}
}