———— of a daily province the power set of a set by recursion + backtracking

Source: Internet
Author: User

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);


 }
    }

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.