package DataStructures.DisjointSets; public class DisjointSets { public Node MakeSet(T x) { return new Node(x); }; public Node FindSet(Node node) { if (node != node.parent) { node.parent = FindSet(node.parent); } return node.parent; } public void UnionSet(Node x, Node y) { Node nx = FindSet(x); Node ny = FindSet(y); if (nx == ny) { return; } if (nx.rank > ny.rank) { ny.parent = nx; } else if (ny.rank > nx.rank) { nx.parent = ny; } else { nx.parent = ny; ny.rank++; } } }