static LinkedList <String> GenerateBinaryRepresentationList(int n) { LinkedQueue <StringBuilder> q = new LinkedQueue <StringBuilder>(); LinkedList <String> output = new System.Collections.Generic.LinkedList <String>(); if (n < 1) { return(output); } q.Push(new StringBuilder("1")); while (n-- > 0) { StringBuilder sb = q.Pop(); //add to output list "not sure Addafter or Addlast" output.AddLast(sb.ToString()); //copy StringBuilder sbc = new StringBuilder(sb.ToString()); //left sb.Append('0'); q.Push(sb); //right sbc.Append('1'); q.Push(sbc); } return(output); }
static LinkedList <string> GenerateBinaryRepresentationList(int n) { LinkedQueue <StringBuilder> q = new LinkedQueue <StringBuilder>(); LinkedList <String> output = new LinkedList <String>(); if (n < 1) { return(output); } q.Push(new StringBuilder("1")); while (n-- > 0) { StringBuilder sb = q.Pop(); output.AddLast(sb.ToString()); StringBuilder sbc = new StringBuilder(sb.ToString()); sb.Append('0'); q.Push(sb); sbc.Append('1'); q.Push(sbc); } return(output); }
static LinkedList <string> GenerateBinaryRepresentationList(int n) { ///<value> ///Create an empty queue of strings with which to perform the traversal ///</value> LinkedQueue <StringBuilder> q = new LinkedQueue <StringBuilder>(); ///<value> ///A list for returning the binary values ///</value> LinkedList <String> output = new LinkedList <String>(); if (n < 1) { ///<return> /// binary representation of negative values is not supported return an empty list ///</return> return(output); } /// Enqueue the first binary number. Use a dynamic string to avoid string concat q.Push(new StringBuilder("1")); /// BFS while (n-- > 0) { /// print the front of queue StringBuilder sb = q.Pop(); output.AddLast(sb.ToString()); /// Make a copy StringBuilder sbc = new StringBuilder(sb.ToString()); /// Left child sb.Append('0'); q.Push(sb); /// Right child sbc.Append('1'); q.Push(sbc); } return(output); }
static LinkedList <string> generateBinaryRepList(int n) { ///Creates an empty queue of strings LinkedQueue <StringBuilder> x = new LinkedQueue <StringBuilder>(); ///The list is returning the binary values LinkedList <string> output = new LinkedList <string>(); if (n < 1) { /// binary values and representation do not support negative values /// return an empty list return(output); } ///This will add the first binary number. This uses a dynamic string ///to avoid string concat. x.Push(new StringBuilder("1")); ///BFS while (n > 0) { ///print the first part of the queue StringBuilder sb = x.Pop(); output.AddLast(sb.ToString()); ///Make a copy StringBuilder sbc = new StringBuilder(sb.ToString()); ///Left Child sb.Append("0"); x.Push(sb); ///Right Child sbc.Append("1"); x.Push(sbc); n--; } return(output); }