Exemple #1
0
 public void Unite(UnionFindNode<T> other) {
     UnionFindNode<T> a = this.Find();
     UnionFindNode<T> b = other.Find();
     if (a != b) {
         // need to unite a and b - a will be parent of b
         int aSize = a.subNodes == null ? 1 : a.subNodes.Count;
         int bSize = b.subNodes == null ? 1 : b.subNodes.Count;
         if (aSize < bSize) { // a is smaller, so it should be child
             UnionFindNode<T> t = a;
             a = b;
             b = t;
         }
         b.parent = a;
         if (a.subNodes == null) {
             a.subNodes = new List<UnionFindNode<T>>();
             a.subNodes.Add(a);
         }
         if (b.subNodes == null) {
             a.subNodes.Add(b);
         } else {
             foreach (UnionFindNode<T> bn in b.subNodes) {
                 a.subNodes.Add(bn);
             }
             b.subNodes = null;
         }
         allRoots.Remove(b);
     }
 }
Exemple #2
0
 public bool IsInSameSet(UnionFindNode<T> other) {
     return this.Find() == other.Find();
 }