Exemplo n.º 1
0
        public static DisJointSetNode find(DisJointSetNode node)
        {
            var parent = node.parent;

            if (parent == node)
            {
                return(node);
            }

            node.parent = find(node.parent);
            return(node.parent);
        }
Exemplo n.º 2
0
        public static void union(DisJointSetNode node1, DisJointSetNode node2)
        {
            var par1 = find(node1);
            var par2 = find(node2);

            if (par1 == par2)
            {
                return;
            }

            if (par1.rank > par2.rank)
            {
                par2.parent = par1;
            }
            else if (par1.rank == par2.rank)
            {
                par2.parent = par1;
                par1.rank++;
            }
            else
            {
                par1.parent = par2;
            }
        }