예제 #1
0
                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);
                    }
                }
예제 #2
0
                public UnionFindNode FindRoot()
                {
                    if (_parent != this) // not ref equals
                    {
                        var root = _parent.FindRoot();
                        _parent = root;
                    }

                    return(_parent);
                }
예제 #3
0
 public bool IsInSameGroup(UnionFindNode other) => this.FindRoot() == other.FindRoot();
예제 #4
0
 public bool IsInSameGroup(UnionFindNode other)
 {
     return(this.FindRoot() == other.FindRoot());
 }