|
@@ -1,54 +0,0 @@
|
1
|
|
-package rocks.zipcode.io.quiz4.utils;
|
2
|
|
-
|
3
|
|
-import rocks.zipcode.io.quiz4.collections.ComparableTreeSet;
|
4
|
|
-
|
5
|
|
-import java.util.*;
|
6
|
|
-
|
7
|
|
-/**
|
8
|
|
- * @author leon on 18/12/2018.
|
9
|
|
- */
|
10
|
|
-public class MyTestClass {
|
11
|
|
-
|
12
|
|
- public static <E> Set<Set<E>> findPowerSet(Set<E> set) {
|
13
|
|
- Set<Set<E>> ret = new TreeSet<>();
|
14
|
|
- ret.add(set);
|
15
|
|
- if (set.isEmpty()) {
|
16
|
|
- return ret;
|
17
|
|
- }
|
18
|
|
- Iterator<E> it = set.iterator();
|
19
|
|
- while (it.hasNext()) {
|
20
|
|
- Set<E> tmp = (Set)new ComparableTreeSet(set); //create a copy of current set
|
21
|
|
- tmp.remove(it.next()); //remove current element from copy set
|
22
|
|
- ret.add(tmp); //add the remaining set to result
|
23
|
|
- ret.addAll(findPowerSet(tmp)); //recursively find subsets of copy set
|
24
|
|
- }
|
25
|
|
- return ret;
|
26
|
|
- }
|
27
|
|
-
|
28
|
|
- public static void main(String[] args) {
|
29
|
|
- Set<Character> set = (Set)new ComparableTreeSet<Character>();
|
30
|
|
- set.add('x');
|
31
|
|
- set.add('y');
|
32
|
|
- set.add('z');
|
33
|
|
- set.add('t');
|
34
|
|
- System.out.println("Input set");
|
35
|
|
- printSet(set);
|
36
|
|
- System.out.println("\nsub sets");
|
37
|
|
- findPowerSet(set).stream().forEach(MyTestClass::printSet);
|
38
|
|
- }
|
39
|
|
-
|
40
|
|
-
|
41
|
|
- public static <E> void printSet(E... val) {
|
42
|
|
- printSet(new TreeSet<>(Arrays.asList(val)));
|
43
|
|
- }
|
44
|
|
-
|
45
|
|
- public static <E> void printSet(Set<E> set) {
|
46
|
|
- StringBuilder sb = new StringBuilder(set.size() == 0 ? "{}\n" : "{");
|
47
|
|
- Iterator<E> it = set.iterator();
|
48
|
|
- while (it.hasNext()) {
|
49
|
|
- sb.append(it.next().toString())
|
50
|
|
- .append(it.hasNext() ? ", " : "}\n");
|
51
|
|
- }
|
52
|
|
- System.out.print(sb.toString());
|
53
|
|
- }
|
54
|
|
-}
|