Ejemplo n.º 1
0
        /// <summary>
        /// This will print the binary repesentation of all numbers from 1 up to n
        /// By doing this it will design a virtual binary tree that will perform
        /// a level order traversal using the FIFO queue.
        /// Result should be like this:
        ///         1
        ///     /       \
        ///    10       11
        ///   /  \     /  \
        /// 100  101 110  111
        ///
        /// etc.
        /// which will store each value that was visited in the list.
        /// </summary>
        static LinkedList <string> generateBinaryRepresentationList(int n)
        {
            /// <summary>
            /// Creates an empty queue of strings
            /// </summary>
            LinkedQueue <StringBuilder> x = new LinkedQueue <StringBuilder>();

            /// <summary>
            /// The list is returning the binary values
            /// </summary>
            LinkedList <string> output = new LinkedList <string>();

            if (n < 1)
            {
                /// <summary>
                /// binary values and representation do not support negative values
                /// return an empty list
                /// </summary>
                return(output);
            }

            /// <summary>
            /// This will add the first binary number. This uses a dynamic string
            /// to avoid string concat.
            /// </summary>
            x.push(new StringBuilder("1"));

            while (n > 0)
            {
                /// <summary>
                /// print the first part of the queue
                /// </summary>
                StringBuilder sb = x.pop();
                output.AddLast(sb.ToString());

                /// < summary >
                /// Make a copy
                /// </summary>
                StringBuilder sbc = new StringBuilder(sb.ToString());

                /// <summary>
                /// Left Child
                /// </summary>
                sb.Append("0");
                x.push(sb);

                /// <summary>
                /// Right Child
                /// </summary>
                sbc.Append("1");
                x.push(sbc);
                n--;
            }
            return(output);
        }
Ejemplo n.º 2
0
        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);
        }