-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnionFind.java
More file actions
72 lines (62 loc) · 1.31 KB
/
UnionFind.java
File metadata and controls
72 lines (62 loc) · 1.31 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// AUTHOR: Soel Micheletti
class Node<T>{
T item;
Node parent;
public Node(T item) {
this.item = item;
}
}
public class UnionFind<T> {
public void makeSet(Node n) {
n.parent = n;
}
public Node find(Node a) {
if(a.parent.equals(a))
return a;
else {
Node tmp = find(a.parent);
a.parent = tmp;
return tmp;
}
}
public void union(Node a, Node b) {
Node t1 = find(a);
Node t2 = find(b);
a.parent = b;
}
public static void main(String[]args) {
UnionFind<Integer> U = new UnionFind<Integer>();
Node a = new Node("a");
Node b = new Node("b");
Node c = new Node("c");
Node d = new Node("d");
Node e = new Node("e");
Node f = new Node("f");
Node g = new Node("g");
Node k = new Node("k");
U.makeSet(a);
U.makeSet(b);
U.makeSet(c);
U.makeSet(d);
U.makeSet(e);
U.makeSet(f);
U.makeSet(g);
U.makeSet(k);
b.parent = a;
c.parent = a;
g.parent = b;
k.parent = g;
d.parent = b;
e.parent = d;
f.parent = d;
U.find(e);
System.out.println(a.parent.item);
System.out.println(e.parent.item);
System .out.println(d.parent.item);
System.out.println(f.parent.item);
System.out.println(b.parent.item);
System.out.println(g.parent.item);
System.out.println(k.parent.item);
System.out.println(c.parent.item);
}
}