public bool Unite(UnionFindNode other) { var thisRoot = this.FindRoot(); var otherRoot = other.FindRoot(); if (thisRoot == otherRoot) { return(false); } if (thisRoot._height < otherRoot._height) { thisRoot._parent = otherRoot; otherRoot._groupSize += thisRoot._groupSize; otherRoot._height = Math.Max(thisRoot._height + 1, otherRoot._height); return(true); } else { otherRoot._parent = thisRoot; thisRoot._groupSize += otherRoot._groupSize; thisRoot._height = Math.Max(otherRoot._height + 1, thisRoot._height); return(true); } }
public UnionFindNode FindRoot() { if (_parent != this) // not ref equals { var root = _parent.FindRoot(); _parent = root; } return(_parent); }
public bool IsInSameGroup(UnionFindNode other) => this.FindRoot() == other.FindRoot();
public bool IsInSameGroup(UnionFindNode other) { return(this.FindRoot() == other.FindRoot()); }