コード例 #1
0
        private UnionFindNode <T> FindRoot()
        {
            if (_parent != this) // not ref equals
            {
                var root = _parent.FindRoot();
                _parent = root;
            }

            return(_parent);
        }
コード例 #2
0
        public void Unite(UnionFindNode <T> other)
        {
            var thisRoot  = this.FindRoot();
            var otherRoot = other.FindRoot();

            if (thisRoot == otherRoot)
            {
                return;
            }

            if (thisRoot._height < otherRoot._height)
            {
                thisRoot._parent      = otherRoot;
                otherRoot._groupSize += thisRoot._groupSize;
                otherRoot._height     = Math.Max(thisRoot._height + 1, otherRoot._height);
            }
            else
            {
                otherRoot._parent    = thisRoot;
                thisRoot._groupSize += otherRoot._groupSize;
                thisRoot._height     = Math.Max(otherRoot._height + 1, thisRoot._height);
            }
        }
コード例 #3
0
 public bool IsInSameGroup(UnionFindNode <T> other) => this.FindRoot() == other.FindRoot();