Beispiel #1
0
        /// <summary>
        /// Creates a new set with given member
        /// </summary>
        /// <param name="member"></param>
        public void MakeSet(T member)
        {
            var newSet = new DisJointSetNode <T>()
            {
                Data = member,
                Rank = 0
            };

            //Root's Parent is Root itself
            newSet.Parent = newSet;

            set.Add(member, newSet);
        }
Beispiel #2
0
        /// <summary>
        /// Recursively move up in the set tree till root
        /// And returns the Root (reference member)
        /// Do Path Compression on all visited members on way to root
        /// By pointing their parent to Root
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        private DisJointSetNode <T> FindSet(DisJointSetNode <T> node)
        {
            var parent = node.Parent;

            if (node != parent)
            {
                //compress path by setting parent to Root
                node.Parent = FindSet(node.Parent);
                return(node.Parent);
            }
            else
            {
                //reached root so return the Root (reference Member)
                return(parent);
            }
        }