public bool Union(UnionFindNode p2) { var root1 = Find(); var root2 = p2.Find(); if (ReferenceEquals(root1, root2)) { return(false); } if (root1.rank < root2.rank) { root1._parent = root2; } else if (root1.rank > root2.rank) { root2._parent = root1; } else { root2._parent = root1; root1.rank++; } return(true); }
/// <summary> /// Determines whether or not this node and the other node are in the same set. /// </summary> public bool IsUnionedWith(UnionFindNode <T> other) { if (other == null) { throw new ArgumentNullException("other"); } return(ReferenceEquals(Find(), other.Find())); }
/// <summary> /// Returns the current representative of the set this node is in. /// Note that the representative is only accurate untl the next Union operation. /// </summary> public UnionFindNode <T> Find() { if (!ReferenceEquals(_parent, this)) { _parent = _parent.Find(); } return(_parent); }
public bool IsUnionWith(UnionFindNode p2) { return(ReferenceEquals(Find(), p2.Find())); }