Generating GreyCode Recursively

61 Views Asked by At

so Im trying to make a recursive solution to a hypercube where a walk method allows you to walk to every point in a hypercube. for example if you put walk(3) the console should output:

000, 001, 011, 010, 110, 111, 101, 100

I have made an iterative solution for this problem using greycode, as the mentality I went with was that each point is only 1 bit away from the other, so i just have to bit shift once every loop. I then attempted to do the same but in a recursive way. I am not allowed to use arrays, lists, stacks, etc any type of ques and here is what I came out with as my recursive solution:

RECURSIVE SOLUTION:

   public static void recursiveWalk(int n)
   {
      if (n < 1)
      {
         System.out.print("no");
      }
      else
      {
         String ass = "";
         recursiveWalkHelper(n, 0, ass);
         System.out.print(ass);
      }
   }

   public static void recursiveWalkHelper(int n, int i, String str)
   {
      int N = 1 << n;

      if (i < N)
      {
         int x = i ^ (i >> 1);
         getGrayCode(x, n, str, 0, 0, 0);
         System.out.println();
         i++;
         recursiveWalkHelper(n, i, str);
      }
      return;
   }

   public static void getGrayCode(int x, int n, String str, int i, int j, int k)
   {

      if (x > 0)
      {
         str = str + x % 2;
         x = x / 2;
         i++;
         getGrayCode(x, n, str, i, j, k);
      }

      if (j < n - i)
      {
         System.out.print('0');
         j++;
         getGrayCode(x, n, str, i, j, i - 1);
         return;
      }

      if (k >= 0)
      {
         System.out.print(str.charAt(k));
         i--;
         getGrayCode(x, n, str, i, j, k);
         return;
      }

      return;
   }

However the output for the code is as follows when I put recursiveWalk(3) into the main:

000
00100010
0101001010010101010
0100001000000000000
0010000010000110001100000000000000000000000
1010101010100110101101001101011010110101010
1000101000100000100001001101011010110101010
0000000000000000000000000000000000000000000

which isn't what I'm expecting. I want to emphasize that I am not meant to utilize any form of ques, so I've only been using strings and simple printouts

Here is the Iterative solution that does work that I came out with:

   static void iterativehelper(int x, int n)
   {
      String ass = "";
      int i = 0;

      for (i = 0; x > 0; i++)
      {
         ass = ass + x % 2;
         x = x / 2;
      }

      // leftmost digits are
      // filled with 0
      for (int j = 0; j < n - i; j++)
      {
         System.out.print('0');
      }

      for (int j = i - 1; j >= 0; j--)
      {
         System.out.print(ass.charAt(j));
      }
   }

   // Function to generate
   // gray code
   static void iterative(int n)
   {
      int N = 1 << n;
      for (int i = 0; i < N; i++)
      {
         // generate gray code of
         // corresponding binary
         // number of integer i.
         int x = i ^ (i >> 1);

         // printing gray code
         iterativehelper(x, n);
         System.out.println();
      }
   }
0

There are 0 best solutions below