Beispiel #1
0
 public UnionFindNode(T item)
 {
     _height    = 0;
     _groupSize = 1;
     _parent    = this;
     Item       = item;
 }
Beispiel #2
0
        private UnionFindNode <T> FindRoot()
        {
            if (_parent != this) // not ref equals
            {
                var root = _parent.FindRoot();
                _parent = root;
            }

            return(_parent);
        }
Beispiel #3
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);
            }
        }
Beispiel #4
0
 public bool IsInSameGroup(UnionFindNode <T> other) => this.FindRoot() == other.FindRoot();