@Test public void testUnion() { DisjointSets<Integer> sets = new DisjointSets<Integer>(newRange(0, 10)); sets.union(0, 8); assertTrue(sets.areTogether(0, 8)); sets.union(0, 4); assertTrue(sets.areTogether(0, 4)); assertTrue(sets.areTogether(4, 8)); sets.union(1, 3); sets.union(5, 7); sets.union(3, 5); assertTrue(sets.areTogether(1, 7)); sets.union(5, 4); assertTrue(sets.areTogether(0, 7)); assertTrue(sets.areTogether(4, 5)); }
public void union(E e1, E e2) { Entry<E> r1 = map.get(e1).findRepresentative(); Entry<E> r2 = map.get(e2).findRepresentative(); if (r1 != r2) { if (r1.size <= r2.size) { r2.mergeWith(r1); } else { r1.mergeWith(r2); } } }
DisjointSets implements Iterable<Set<E>> { public void union(E e1, E e2) { Entry<E> r1 = map.get(e1).findRepresentative(); Entry<E> r2 = map.get(e2).findRepresentative(); if (r1 != r2) { if (r1.size <= r2.size) { r2.mergeWith(r1); } else { r1.mergeWith(r2); } } } }
DisjointSets implements Iterable<Set<E>> { public void union(E e1, E e2) { Entry<E> r1 = map.get(e1).findRepresentative(); Entry<E> r2 = map.get(e2).findRepresentative(); if (r1 != r2) { if (r1.size <= r2.size) { r2.mergeWith(r1); } else { r1.mergeWith(r2); } } } DisjointSets(Collection<? extends E> c); }
DisjointSets implements Iterable<Set<E>> { public void union(E e1, E e2) { Entry<E> r1 = map.get(e1).findRepresentative(); Entry<E> r2 = map.get(e2).findRepresentative(); if (r1 != r2) { if (r1.size <= r2.size) { r2.mergeWith(r1); } else { r1.mergeWith(r2); } } } DisjointSets(Collection<? extends E> c); boolean areTogether(E e1, E e2); void union(E e1, E e2); @Override Iterator<Set<E>> iterator(); static DisjointSets<E> singletonsOf(Class<E> elementType); }
DisjointSets implements Iterable<Set<E>> { public void union(E e1, E e2) { Entry<E> r1 = map.get(e1).findRepresentative(); Entry<E> r2 = map.get(e2).findRepresentative(); if (r1 != r2) { if (r1.size <= r2.size) { r2.mergeWith(r1); } else { r1.mergeWith(r2); } } } DisjointSets(Collection<? extends E> c); boolean areTogether(E e1, E e2); void union(E e1, E e2); @Override Iterator<Set<E>> iterator(); static DisjointSets<E> singletonsOf(Class<E> elementType);  }