Generate Binary Strings of length N using Branch and Bound | Java Program to generate Binary Strings using Branch and Bound ; Creating a Node class ; Queue that maintains the list of live Nodes ; Utility function to generate binary strings of length n ; If list is full print combination ; Create a new vector for new combination ; iterate while length is not equal to n ; Driver code ; Initiate Generation Create a root Node ; Instantiate the Queue
import java . io . * ; import java . util . * ; class Node { int soln [ ] ; int level ; ArrayList < Node > child ; Node parent ; Node ( Node parent , int level , int N ) { this . parent = parent ; this . level = level ; this . soln = new int [ N ] ; } } class GFG { static int N ; public static Queue < Node > Q ; public static void generate ( Node n ) { if ( n . level == N ) { for ( int i = 0 ; i <= N - 1 ; i ++ ) { System . out . print ( n . soln [ i ] ) ; } System . out . println ( ) ; } else { n . child = new ArrayList < Node > ( ) ; int l = n . level ; for ( int i = 0 ; i <= 1 ; i ++ ) { Node x = new Node ( n , l + 1 , N ) ; for ( int k = 0 ; k < l ; k ++ ) { x . soln [ k ] = n . soln [ k ] ; } x . soln [ l ] = i ; n . child . add ( x ) ; Q . add ( x ) ; } } } public static void main ( String args [ ] ) { N = 3 ; Node root = new Node ( null , 0 , N ) ; Q = new LinkedList < Node > ( ) ; Q . add ( root ) ; while ( Q . size ( ) != 0 ) { Node E = Q . poll ( ) ; generate ( E ) ; } } }