Generate Binary Strings of length N using Branch and Bound | C # 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
using System ; using System . Collections . Generic ; public class Node { public int [ ] soln ; public int level ; public List < Node > child ; public Node parent ; public 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 ++ ) { Console . Write ( n . soln [ i ] ) ; } Console . WriteLine ( ) ; } else { n . child = new List < 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 . Enqueue ( x ) ; } } } public static void Main ( String [ ] args ) { N = 3 ; Node root = new Node ( null , 0 , N ) ; Q = new Queue < Node > ( ) ; Q . Enqueue ( root ) ; while ( Q . Count != 0 ) { Node E = Q . Dequeue ( ) ; generate ( E ) ; } } }