Пример #1
0
        public DisjointSet <T> Find()
        {
            if (this.root != this)
            {
                this.root = this.root.Find();
            }

            return(this.root);
        }
Пример #2
0
        public void Union(DisjointSet <T> other)
        {
            var thisRoot  = Find();
            var otherRoot = other.Find();

            if (thisRoot == otherRoot)
            {
                return;
            }

            if (thisRoot.rank < otherRoot.rank)
            {
                thisRoot.root = otherRoot;
            }
            else if (thisRoot.rank > otherRoot.rank)
            {
                otherRoot.root = thisRoot;
            }
            else
            {
                otherRoot.root = thisRoot;
                thisRoot.rank++;
            }
        }
Пример #3
0
 public DisjointSet()
 {
     root = this;
 }