Example #1
0
    int merge(UFNode x) {
      x = x.find();
      UFNode y = this.find();

      if (x != y) {
        if (x.size < y.size) {
          UFNode temp = x;
          x = y;
          y = temp;
        }
        x.size += y.size;
        y.size = 0;
        y.parent = x;
        x.children.add(y);
      }

      return x.size;
    }
Example #2
0
 void remove() {
   UFNode root = this.find();
   if (root != this) for (UFNode child : children) child.parent = root;
   else if (!children.isEmpty()) {
     Iterator<UFNode> it = children.iterator();
     root = it.next();
     root.parent = null;
     while (it.hasNext()) it.next().parent = root;
     root.size = this.size;
   }
   root.size--;
   this.parent = null;
   this.size = 1;
 }