This class is used to create sets out of nodes to from a graph-like structure (or grouping since I remove most of the normal graph structure and all child nodes have the same parent).
Is there anything I can do to improve the design of this class? Is the use of static methods here appropriate?
class UnionFind(object):
'''
Used to initialize the set and then holds the required functions
to operate on them
'''
def __init__(self, vertex):
self.rank = 0
self.parent = self
self.vertex = vertex
@staticmethod
def find(x):
if x != x.parent:
x.parent = UnionFind.find(x.parent)
return x
@staticmethod
def union(x,y):
xroot = UnionFind.find(x)
yroot = UnionFind.find(y)
if xroot == yroot:
return
if xroot.rank > yroot.rank:
yroot.parent = xroot
else:
xroot.parent = yroot
if xroot.rank == yroot.rank:
yroot.rank += 1