Import java.util.ArrayList;
Import java.util.List;
The/** * called Power set, which is a set of all subsets of a set (including the complete and empty sets) as elements. * This class solves the power set of a set by traversing a tree that is full of two forks. The principle of a program is to consider the process of a power set element as a first-order traversal of a two-forked tree with a depth of n+1, * Starting with the root node, access to the left child represents the power set element (a subset of the collection) that contains the first element of the collection, and access to the right child represents the * first element of the power set element that does not contain the collection, so that In the second layer of the binary tree complete the selection of the first element of the set, and so on, and so on, when the traversal reached the n+1 layer, that is, the two-fork Tree leaf * child node, complete the collection of all elements of the choice, then output a trade-off power set elements.
Full two fork tree n+1 layer has 2n leaf node, represents the set of 2n power set elements, to traverse the output of the complete two-tree leaf node, also got the power set we require.
*/public class Powersetfinder<v> {public static void main (string[] args) {//Initialize a collection, put it in the list
list<string> list = new arraylist<string> ();
List.add ("A");
List.add ("B");
List.add ("C");
List.add ("D");
list<string> li = new arraylist<string> ();
Printpowerset (0, List, Li);
System.out.println ();
System.out.println ("-------------------------------------------------");
System.out.println ();
List<integer> List1 = new arraylist<integer> (); LisT1.add (1);
List1.add (2);
List1.add (3);
List1.add (4);
list<integer> li1 = new arraylist<integer> ();
Printpowerset (0, List1, li1); /** * recursive + backtracking exponentiation set * @param i * @param list * @param li * @author lhever February 21, 2017 11:50 : * @since v1.0/public static <V> void Printpowerset (int i, list<v> List, list<v> Li
{if (I > (List.size ()-1)) {System.out.println (LI); else {Li.add (List.get (i));//Zoga Printpowerset (i + 1, list, li);//Recursive method Li. Remove (List.get (i));
Right Go printpowerset (i + 1, list, Li);
}
}
}